17 Jul 2011

Cakephp form input using select with options

There are times that you need to control the form->input helper with specific type setting and additional options. This is probably more comment when building custom form inputs that aren’t automagically being set from the model/controller.

I recently had to build a list on distinct dates, but prefer to user $this->Form->input, rather than $this->Form->select. I first built an indexed array containing my date fields.

array(
[xxxx-xx-xx] => xxxx-xx-xx
[yyyy-yy-yy] => yyyy-yy-yy
)

We’ll call this array $options. You can then build the input using something like

$this->form->input('Input Name', array(
  'type' => 'select',
  'options' => 'options',
  'label' => 'label',
  'empty' => 'No data selected'
);

Leave a Reply