Fieldset Class

The Fieldset class is used to create a form and handle its validation in an object oriented way. It uses the Form and Validation classes. This class itself is only meant to model the fieldset and its fields while the other two classes do the real work.

The resulting form markup is generated when passing the Fieldset instance to a View or by calling build().

forge($name = 'default', $config = array())

The forge method returns a new Fieldset instance. Note: there can only be one instance per $name.

Static Yes
Parameters
Param Default Description
$name
'default'
Identifier for this fieldset
$config array() Configuration array
Returns \Fieldset Object
Example
$article_form = Fieldset::forge('article');

instance($instance = null)

Returns a specific instance, or the default instance (is created if necessary).

Static Yes
Parameters
Param Default Description
$instance
null
Identifier of the fieldset instance you want to retrieve
Returns \Fieldset Object
or false if the specified instance doesn't exist.
Example
$article_form = Fieldset::instance('article');

validation()

Gets the Validation instance for the current Fieldset. Creates the Validation instance if it doesn't already exist.

Static No
Returns \Validation Object
Example
$validation = $article_form->validation();

form()

Gets the Form instance for the current Fieldset. Creates the Form instance if it doesn't already exist.

Static No
Returns \Form Object
Example
$form = $article_form->form();

add($name, $label = '', array $attributes = array(), array $rules = array())

Creates a Fieldset_Field instance and adds it to the current Fieldset.

Static No
Parameters
Param Default Description
$name required HTML name attribute, also used for referencing the field within the Fieldset
$label
''
Field label
$attributes
array()
HTML attributes as an associative array
$rules
array()
Validation rules to be applied to this Field
Returns \Fieldset_Field Object
Example
$title_field = $article_form->add('article_title', 'Title', array('class' => 'pretty_input'));

// Example Radio
$ops = array('male', 'female');
$form->add('gender', '', array('options' => $ops, 'type' => 'radio', 'value' => 'true'));

// Example Checkbox
$ops = array('male', 'female');
$form->add('gender', '', array('options' => $ops, 'type' => 'checkbox', 'value' => 'true'));

// Example of an Email input
$form->add('email', 'E-mail', array('type' => 'email', 'class' => 'pretty_input'),  array(array('required'), array('valid_email')));

add_before($name, $label = '', array $attributes = array(), array $rules = array(), $fieldname = null)

Creates a Fieldset_Field instance and adds it to the current Fieldset, just before an already defined field identified by $fieldname.

Static No
Parameters
Param Default Description
$name required HTML name attribute, also used for referencing the field within the Fieldset
$label
''
Field label
$attributes
array()
HTML attributes as an associative array
$rules
array()
Validation rules to be applied to this Field
$fieldname
null
Already defined field to which this field must be prepended
Returns \Fieldset_Field Object
Example
// Radio button field, to be added before the 'location' field.
$ops = array('male', 'female');
$form->add_before('gender', '', array('options' => $ops, 'type' => 'radio', 'value' => 'true'), array(), 'location');

add_after($name, $label = '', array $attributes = array(), array $rules = array(), $fieldname = null)

Creates a Fieldset_Field instance and adds it to the current Fieldset, just after an already defined field identified by $fieldname.

Static No
Parameters
Param Default Description
$name required HTML name attribute, also used for referencing the field within the Fieldset
$label
''
Field label
$attributes
array()
HTML attributes as an associative array
$rules
array()
Validation rules to be applied to this Field
$fieldname
null
Already defined field to which this field must be appended
Returns \Fieldset_Field Object
Example
// Radio button field, to be added after the 'location' field.
$ops = array('male', 'female');
$form->add_after('gender', '', array('options' => $ops, 'type' => 'radio', 'value' => 'true'), array(), 'location');

field($name = null)

Gets one or all Fieldset_Field instances for the current Fieldset.

Static No
Parameters
Param Default Description
$name
null
The name of an existing field in this Fieldset or null to get all fields.
Returns \Fieldset_Field Object
or array() of Fieldset_Fields
Example
$fields = $article_form->field();
$title_field = $article_form->field('article_title');

add_model($class, $instance = null, $method = 'set_form_fields')

Add a model's fields. The model must have a method set_form_fields() that takes this Fieldset instance and adds fields to it.
Orm\Model have support this build in.

Static No
Parameters
Param Default Description
$class required Either a full classname (including full namespace) or object instance of the model to get fields from.
$instance
null
Array or object that has the exactly same named properties to populate the fields. (Takes the field names from the model and fetches the remaining parameters from this array/object.
$method
'set_form_fields'
The name of the method name to call on the model for field fetching.
Returns \Fieldset Object
Example
$article_form = Fieldset::forge('article');
$article_form->add_model('Model_Article');

set_config($config, $value = null)

Sets a config value on the fieldset.

Static No
Parameters
Param Default Description
$config required Configuration array.
$value
null
If specified, sets the item to set from the passed Configuration array.
Returns \Fieldset Object
Example
$article_form->set_config($config);

// or

$article_form->set_config('key', 'value');

get_config($key = null, $default = null)

Get the configuration array or one or more config values by key.

Static No
Parameters
Param Default Description
$key
null
A single key or multiple in an array, empty to fetch all.
$default
null
A single config value or multiple in an array if $key input is an array to be returned if the items aren't found.
Returns array() or mixed
Example
$config = $article_form->get_config();

populate($input, $repopulate = false)

Set initial field values to the given input, optionally set it to repopulate after that as well.

Static No
Parameters
Param Default Description
$input required An object or associative array of values to assign to their respective Fields, or a Model instance to take the values from.
$repopulate false Use the repopulate() method after initial populating
Returns \Fieldset Object
Example
$article_form->populate($model);

repopulate()

Set all field values to the input send by a form submission (uses the form method attribute to decide wether to check POST or GET).

Static No
Parameters None
Returns \Fieldset Object
Example
$article_form->repopulate();

build($action = null)

Alias for $this->form()->build() for this fieldset. Generates the HTML form markup. See Form.

Static No
Parameters
Param Default Description
$action
null
A URL for the action attribute of the form.
Returns string HTML markup
Example
$this->template->form = $article_form->build(Uri::create('article/submit'));

input($field = null)

Alias for $this->validation()->input(). Gets an array of validated input value(s) from either POST data or given input. See Validation.

Static No
Parameters
Param Default Description
$field
null
A specific field for which to get the input.
Returns Array of input or string value of specified field
Example
$input_values = $article_form->input();

validated($field = null)

Alias for $this->validation()->validated(). Gets an array of input value(s) that passed validation. See Validation.

Static No
Parameters
Param Default Description
$field
null
A specific field for which to get the input.
Returns Array of input or string value of specified field, or false if the field isn't found
Example
$validated_values = $article_form->validated();

error($field = null)

Alias for $this->validation()->error(). Gets an array of input value(s) that did not pass validation. See Validation.

Static No
Parameters
Param Default Description
$field
null
A specific field for which to get the input.
Returns Array of input or string value of specified field, or false if the field isn't found
Example
$errors = $article_form->error();

show_errors(Array $config = array())

Alias for $this->validation()->show_errors(). Returns all errors in a list or with set markup from the $config parameter. See Validation.

Static No
Parameters
Param Default Description
$config
array()
Uses keys open_list, close_list, open_error, close_error & no_errors. Overrides the values
Returns Array of input or string value of specified field, or false if the field isn't found
Example
$validated_values = $article_form->validated();

Creating Forms with ORM Models

The Fieldset class can be used to create forms from ORM models. The following example will build a form based on the fields specified in $_properties of your model. In the example, $article is an instance of Model_Article.

echo Fieldset::forge('article')->add_model($article)->populate($article, true)->build();

If you do not wish every property of the model to be on your form, you can set the form type of those to false and they will not be generated.
This actually uses the Observer_Validation behind the scenes, but you do not need to add it as an observer for this to work.

Modify Form Attributes

Changing the attributes of the form() instance in the fieldset can be done in two ways. You can either provide your options in the forge() method's attributes array, or modify the form instance directly.

// Use the form_attributes option in the attributes of the Fieldset object
$fieldset = Fieldset::forge('article', array(
	'form_attributes' => array(
		'id' => 'edit_article_form',
		'name' => 'edit_article'
		)
	)
);

// Modify the form instance directly
$fieldset->form()->set_attribute('id', 'edit_article_form');
				

More examples to be written.