Form select default

Permalink
I'm using the form widget documented at
http://documentation.concrete5.org/tutorials/how-to-use-the-form-wi...
to display a 'select' input field. I'd like to specify an initial default other than the first item. I know how to do this in HTML, but not via this widget.

Help!

Gondwana
 
ob7dev replied on at Permalink Best Answer Reply
ob7dev
According to the tutorial it says:
"The form widget handles setting the selected attribute."

Its ok to code your form element in HTML. You will notice people use both the form widget at times, and at other times they code it themselves. It just depends on the need. For example, a block that has a select element needs to have the whatever value was saved to the database display as the selected value. To do this you have to code the element normally, then add ternary operators to each option in the select that will output 'selected' to the tag if its value matches whats saved in the database.
Gondwana replied on at Permalink Reply
Gondwana
Oh, thanks. I did read that line, but I assumed it was talking about setting the value that the user selects, rather than the initial default.

I note that the widget can set the default to match whatever comes out of the database.

I didn't know I could mix in my own HTML; that should do it.
Gondwana replied on at Permalink Reply
Gondwana
Just found another way to do it. Before
echo $form->select(...

put
if ($response == null) $response='myDefaultValue';
ob7dev replied on at Permalink Reply
ob7dev
Another way you can use the form helper for selects:
<?php $mySelectOptions = array('option 1','option 2','option 3','option 4')?>
<?php echo $form->select('mySelect', $mySelectOptions, $mySelect, array('class'=>'form-control'));?>

The nice thing is, the form control will automatically select whichever value has been stored, instead of having the write ternary operators on each select option to determine that.

The link you posted shows how to use it this way correctly.

I realized I've been trying to answer the wrong question all along though. You needed to set the initial selected item to something other than the first one. How did you end up doing it?
atiqursumon replied on at Permalink Reply
atiqursumon
At first your code convert to Html then work anywhere you put it.