GoodForm


Checkbox Input

There are two ways of adding a single checkbox field to your form object:

checkbox (string name [, string value, mixed checked])

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)

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])

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)

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>