CakePHP: setting defaults for select with optgroups

In a regular select, without optgroups, the following code in CakePHP will make a default value:

'selected' => $ value

Once I have optgroups (select tag with headers), how to set the default value? The previous code does not seem to work.

+7
source share
3 answers

you can try the following:

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

resulting in the creation of the following HTML:

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

Now the two option is displayed instead of any other.

+7
source

Do not use "value" or "selected", etc., it will break your forms in POST. if you must use the presentation level, use "default".

the best way is to install them from the controller:

 if ($this->RequestHandler->is('post')) { ... } else { $this->data['Model']['field'] = 2; // eg } 

See http://www.dereuromark.de/2010/06/23/working-with-forms/ for more details.

+5
source
 echo $this->Form->input('point', array( 'label'=>'', 'options'=>$list_of_options, 'value'=>$default_value, 'empty'=>'--select--', 'onchange'=>'some_action();' ) ); 
+2
source

All Articles