Checkbox Input
There are two ways of adding a single checkbox field to your form object:
checkbox (string name [, string value, mixed checked])
- name: defines the elements name attribute, should be unique in the object | required
- value: defines the default field value | optional
- checked: defines the checked status of the field value | optional
You can flag a checkbox as checked by passing non-empty value to the checked parameter or in the checked attribute of the array parameter.
e.g. TRUE, 1, 'checked' or "lollipops"
Example:
$this->goodform->checkbox('spam_me', 1, TRUE);
Will produce the following HTML:
<input name="spam_me" value="1" checked="checked" type="checkbox" />
checkbox (array attributes)
- attributes: an associative array specifying attributes of the field element.
All attributes in this array will be converted to HTML element attributes. GoodForm will not check if these attributes are HTML valid. The only exceptions to this are the Special attributes that are not passed to the elements attribute string.
Example:
$attr = array(
'name' => 'spam_me',
'value' => '1',
'checked' => 'checked'
'class' => 'my-checkbox-class'
);
$this->goodform->checkbox($attr);
Will produce the following HTML:
<input name="spam_me" value="1" class="my-checkbox-class" checked="checked" type="checkbox" />
Checkbox Groups
Goodform also provides methods for adding groups of checkboxes. There are two ways to add a checkbox group to your form.
checkbox_group (string name [, array options, mixed checked])
- name: defines the elements name attribute, should be unique in the object. Goodform will automatically add a [] to each options name. | required
- options: defines the groups checkbox options. Array should specify label => value to allow for multiple options with the same value. | required
- checked: either and array or single value that specifies the values of checked options. | optional
Example:
$pets = array(
'none' => NULL,
'dog' => 1,
'cat' => 2,
'fish' => 3
);
$this->goodform->checkbox_group('pets', $pets, array(1, 3));
Will produce the following HTML:
<div class="input-group">
<input value="" name="pets[]" type="checkbox"/>
<span class="label">none</span>
<input value="1" name="pets[]" type="checkbox" checked="checked"/>
<span class="label">dog</span>
<input value="2" name="pets[]" type="checkbox"/>
<span class="label">cat</span>
<input value="3" name="pets[]" type="checkbox" checked="checked"/>
<span class="label">fish</span>
<div class="clear"></div>
</div>
checkbox_group (array attributes)
- attributes: an associative array specifying attributes of the field element.
The 'checked' attribute can also be passed as a 'value' attribute to make it easier to programatically build your form from models.
Example:
$attr = array(
'name' => 'ice_cream',
'checked' => array(2,3),
'options' => array(
'Vanilla' => 0,
'Chocolate' => 1,
'Strawberry' => 2,
'Coffee' => 3
),
'class' => 'vert-input-group'
);
$this->goodform->checkbox_group($attr);
Will produce the following HTML:
<div class="input-group vert-input-group">
<input value="0" name="ice_cream[]" type="checkbox"/>
<span class="label">Vanilla</span>
<input value="1" name="ice_cream[]" type="checkbox"/>
<span class="label">Chocolate</span>
<input value="2" name="ice_cream[]" type="checkbox" checked="checked"/>
<span class="label">Strawberry</span>
<input value="3" name="ice_cream[]" type="checkbox" checked="checked"/>
<span class="label">Coffee</span>
<div class="clear"></div>
</div>
Checkbox Groups with Option Groups
You can spoof select box option groups by supplying a 2D option array. Each group will be divided by a label.
$attr = array(
'name' => 'fav_ice_cream',
'checked' => array(3,5),
'options' => array(
'Vanilla' => array(
'On its own' => 1,
'And Choc Chips' => 2,
),
'Chocolate' => array(
'On its own' => 3,
'And Mint' => 4,
'And Toffee' => 5
),
'Strawberry' => 6,
'Coffee' => 7
)
);
$this->goodform->checkbox_group($attr);
Will produce the following HTML:
<div class="optgroup">
<p class="optgroup-label">Vanilla</p>
<div class="input-group">
<input value="1" name="fav_ice_cream[]" type="checkbox"/><span class="label">On its own</span>
<input value="2" name="fav_ice_cream[]" type="checkbox"/><span class="label">And Choc Chips</span>
<div class="clear"></div>
</div>
</div>
<div class="optgroup">
<p class="optgroup-label">Chocolate</p>
<div class="input-group">
<input value="3" name="fav_ice_cream[]" type="checkbox" checked="checked"/><span class="label">On its own</span>
<input value="4" name="fav_ice_cream[]" type="checkbox"/><span class="label">And Mint</span>
<input value="5" name="fav_ice_cream[]" type="checkbox"/ checked="checked"><span class="label">And Toffee</span>
<div class="clear"></div>
</div>
</div>
<input value="6" name="fav_ice_cream[]" type="checkbox"/><span class="label">Strawberry</span>
<input value="7" name="fav_ice_cream[]" type="checkbox"/><span class="label">Coffee</span>
<div class="clear"></div>