Dropdown
There are two ways of adding a dropdown field to your form object:
dropdown (string name [, array options, mixed selected])
- name: defines the elements name attribute, should be unique in the object. | required
- options: defines the dropdown options. Array should specify name => value to allow for multiple options with the same value. | required
- selected: either an array or single value that specifies the values of selected options. | optional
Example:
$options = array(
'Ford' => 1,
'Vaxhall' => 2,
'Nissan' => 3,
'Audi' => 4
);
$this->goodform->dropdown('fav_cars', $options, 3);
Will produce the following HTML:
<select name="fav_cars">
<option value="1">Ford</option>
<option value="2">Vaxhall</option>
<option value="3" selected="selected">Nissan</option>
<option value="4">Audi</option>
</select>
dropdown (array attributes)
- attributes: an associative array specifying attributes of the field element.
The 'selected attribute can also be passed as a 'value' attribute to make it easier to programatically build your form from models.
Example:
$attr = array(
'name' => 'like_cars',
'selected' => array(3, 4),
'options' => array(
'Ford' => 1,
'Vaxhall' => 2,
'Nissan' => 3,
'Audi' => 4
),
'size' => 4,
'multiple' => 'multiple'
);
$this->goodform->dropdown($attr);
Will produce the following HTML:
<select name="like_cars" size="4" multiple="multiple">
<option value="1">Ford</option>
<option value="2">Vaxhall</option>
<option value="3" selected="selected">Nissan</option>
<option value="4" selected="selected">Audi</option>
</select>
Option Groups
Option groups can also be defined by setting a 2D option array
Example
$options = array(
'Bus' => 1,
'Cars' => array(
'Ford' => 2,
'Toyota' => 3,
'Audi' => 4,
),
'Bikes' => array(
'Yamaha' => 5,
'Lamboretta' => 6
),
'Trucks' => 7
);
$attr = array(
'name' => 'optgroup',
'options' => $options,
'value' => 2
);
$this->goodform->dropdown($attr);
Will produce the following HTML:
<select name="optgroup" size="4" multiple="multiple">
<option value="1">Bus</option>
<optgroup label="Cars">
<option value="2" selected="selected">Ford</option>
<option value="3">Toyota</option>
<option value="4">Audi</option>
</optgroup>
<optgroup label="Bikes">
<option value="5">Yamaha</option>
<option value="6">Lamboretta</option>
</optgroup>
<option value="7">Trucks</option>
</select>