Form Class

The Form class can both be used to create individual form elements or to create a full form along with validation, the latter is done in combination with the Fieldset class.

Create individual form elements

open($attributes = array(), $hidden = array())

Create a form open tag.

Static Yes
Parameters
Param Default Description
$attributes
array()
Either a string which will be the action attribute or an array of settings, if action isn't given it will use the current Uri. The attribute's values will be used as html tag properties.
$hidden
array()
An associative array of fieldnames and their values, will all be set as hidden fields.
Returns string
Example
// returns <form action="http://mydomain.com/uri/to/form" accept-charset="utf-8" method="post">
echo Form::open('uri/to/form');

// returns <form action="http://google.com/" accept-charset="utf-8" method="get">
echo Form::open(array('action' => 'http://google.com/', 'method' => 'get'));

close()

Create a form close tag.

Static Yes
Parameters (none)
Returns string
Example
// returns </form>
echo Form::close();

input($field, $value = null, $attributes = array())

Creates an html input element. It can be set using the fieldname, its value and tag attributes or all in one array as the first argument.

Static Yes
Parameters
Param Default Description
$field Required Either a string for the fieldname or an array of tag attributes.
$value
null
Field value, will be ignored when the first param is an array.
$attributes
array()
These will be used as html tag properties.
Returns string
Example
echo Form::input('name', 'value', array('style' => 'border: 2px;'));
// more to be added

button($field, $value = null, $attributes = array())

Creates an html button element. It can be set using the fieldname, its value and tag attributes or all in one array as the first argument.

Static Yes
Parameters
Param Default Description
$field Required Either a string for the fieldname or an array of tag attributes.
$value
null
Field value, will be ignored when the first param is an array.
$attributes
array()
These will be used as html tag properties.
Returns string
Example
echo Form::button('name', 'value', array('style' => 'border: 2px;'));

hidden($field, $value = null, $attributes = array())

This is an alias for Form::input() that automatically sets the type attribute to 'hidden'.

csrf()

Creates an html input element with the value attribute automatically set to the generated CSRF token.

password($field, $value = null, $attributes = array())

This is an alias for Form::input() that automatically sets the type attribute to 'password'.

radio($field, $value = null, $checked = null, $attributes = array())

This is an alias for Form::input() that automatically sets the type attribute to 'radio'.

For backwards compatibility this method also accepts: checkbox($field, $value = null, array $attributes = array())

Static Yes
Parameters
Param Default Description
$field Required Either a string for the fieldname or an array of tag attributes.
$value
null
Field value, will be ignored when the first param is an array.
$checked
null
Whether checked (if bool) or matches $value (if string), will be ignored when the first param is an array.
$attributes
array()
These will be used as html tag properties.
Returns string
Example
echo Form::label('Male', 'gender');
echo Form::radio('gender', 'Male', true);
echo Form::label('Female', 'gender');
echo Form::radio('gender', 'Female');

checkbox($field, $value = null, $checked = null, $attributes = array())

This is an alias for Form::input() that automatically sets the type attribute to 'checkbox'.

For backwards compatibility this method also accepts: checkbox($field, $value = null, array $attributes = array())

Static Yes
Parameters
Param Default Description
$field Required Either a string for the fieldname or an array of tag attributes.
$value
null
Field value, will be ignored when the first param is an array.
$checked
null
Whether checked (if bool) or matches $value (if string), will be ignored when the first param is an array.
$attributes
array()
These will be used as html tag properties.
Returns string
Example
echo Form::label('Male', 'gender');
echo Form::checkbox('gender', 'Male', true);
echo Form::label('Female', 'gender');
echo Form::checkbox('gender', 'Female');

file($field, $attributes = array())

This is an alias for Form::input() that automatically sets the type attribute to 'file'.

reset($field, $value = null, $attributes = array())

This is an alias for Form::input() that automatically sets the type attribute to 'reset'.

submit($field, $value = null, $attributes = array())

This is an alias for Form::input() that automatically sets the type attribute to 'submit'.

textarea($field, $value = null, $attributes = array())

Creates an html textarea element. It can be set using the fieldname, its value and tag attributes or all in one attribute for the first argument.

Static Yes
Parameters
Param Default Description
$field Required Either a string for the fieldname or an array of tag attributes.
$value
null
Field value, will be ignored when the first param is an array.
$attributes
array()
These will be used as html tag properties.
Returns string
Example
echo Form::textarea('description', 'enter here', array('rows' => 6, 'cols' => 8));
// more to be added

select($field, $values = null, $options = array(), $attributes = array())

Creates an html select element. It can be set using the fieldname, its selected value(s), its options and tag attributes or all in one attribute for the first argument.

Static Yes
Parameters
Param Default Description
$field Required Either a string for the fieldname or an array of tag attributes.
$values
null
Selected value or array of values for multiselect, will be ignored when the first param is an array.
$options
array()
Associative array of value=>label pairs, may also contain option groups as opt_name=>array() where the array contains its set of value=>label pairs.
$attributes
array()
These will be used as html tag properties.
Returns string
Example
echo Form::select('country', 'none', array(
	'none' => 'None',
	'europe' => array(
		'uk' => 'United Kingdom',
		'nl' => 'Netherlands'
	),
	'us' => 'United States'
));
// more to be added

label($label, $id = null, $attributes = array())

Creates an html label element. It can be set using label, the id it's for and tag attributes or all in one attribute for the first argument.

Static Yes
Parameters
Param Default Description
$label Required Either a string for the label or an array of tag attributes.
$id
null
The field's id this label belongs to.
$attributes
array()
These will be used as html tag properties.
Returns string
Example
echo Form::label('Username', 'username');
// more to be added

fieldset_open($attributes = array(), $legend = null)

Create a fieldset open tag.

Static Yes
Parameters
Param Default Description
$attributes
array()
An array of settings; may use the key of 'legend' to set the fieldset's legend attribute. The attribute's values will be used as html tag properties.
$legend
string
String to be used for the legend fieldset option
Returns string
Example
// returns <fieldset >
echo Form::fieldset_open();

// returns <fieldset class="example-class" id="example-id">
echo Form::fieldset_open(array('class' => 'example-class', 'id' => 'example-id'));

// returns <fieldset class="example-class" id="example-id"><legend>Custom Legend</legend>
echo Form::fieldset_open(array('class' => 'example-class', 'id' => 'example-id'), 'Custom Legend');

// returns <fieldset class="example-class" id="example-id"><legend>Custom Legend</legend>
echo Form::fieldset_open(array('class' => 'example-class', 'id' => 'example-id', 'legend' => 'Custom Legend'));

fieldset_close()

Create a fieldset close tag.

Static Yes
Parameters (none)
Returns string
Example
// returns </fieldset>
echo Form::fieldset_close();

Create forms in an OOP way using fieldsets

See Fieldset