CakePHP: Setting default value on a select with optgroups

Chaim picture Chaim · Mar 7, 2011 · Viewed 15.8k times · Source

On a normal select, with no optgroups, the following code in CakePHP would make a value the default one:

'selected' => $value

Once I have optgroups (a select tag with headings) how do I set a default value? The previous code doesn't seem to work.

Answer

benjamin picture benjamin · Mar 7, 2011

you might try it like this:

echo $this->Form->input('group_id', array('type'=>'select','default'=>'2'));

leading to the following HTML being generated:

<option value="2" selected="selected">Managers</option>

Now option two is shown instead any other one.