Fieldset クラス

Fieldset クラスは、オブジェクト指向風に、フォームを作ったりそのバリデーションを扱ったりするのに利用されます。 Fieldset クラスは、Form クラスと Validation クラスを利用します。Fieldset クラスそのものは、フィールドセットとそれぞれのフィールドを組み合わせるだけで、 実際には、Form と Validation の 2 つのクラスが仕事をすることになります。

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.

静的 はい
パラメータ
パラメータ 規定値 説明
$name
'default'
Identifier for this fieldset
$config array() Configuration array
返り値 \Fieldset Object
$article_form = Fieldset::forge('article');

instance($instance = null)

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

静的 はい
パラメータ
パラメータ 規定値 説明
$instance
null
Identifier of the fieldset instance you want to retrieve
返り値 \Fieldset Object
or false if the specified instance doesn't exist.
$article_form = Fieldset::instance('article');

validation()

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

静的 No
返り値 \Validation Object
$validation = $article_form->validation();

form()

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

静的 No
返り値 \Form Object
$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.

静的 No
パラメータ
パラメータ 規定値 説明
$name 必須 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
返り値 \Fieldset_Field Object
$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.

静的 No
パラメータ
パラメータ 規定値 説明
$name 必須 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
返り値 \Fieldset_Field Object
// 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.

静的 No
パラメータ
パラメータ 規定値 説明
$name 必須 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
返り値 \Fieldset_Field Object
// 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.

静的 No
パラメータ
パラメータ 規定値 説明
$name
null
The name of an existing field in this Fieldset or null to get all fields.
返り値 \Fieldset_Field Object
or array() of Fieldset_Fields
$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.

静的 No
パラメータ
パラメータ 規定値 説明
$class 必須 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.
返り値 \Fieldset Object
$article_form = Fieldset::forge('article');
$article_form->add_model('Model_Article');

set_config($config, $value = null)

Sets a config value on the fieldset.

静的 No
パラメータ
パラメータ 規定値 説明
$config 必須 Configuration array.
$value
null
If specified, sets the item to set from the passed Configuration array.
返り値 \Fieldset Object
$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.

静的 No
パラメータ
パラメータ 規定値 説明
$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.
返り値 array() or mixed
$config = $article_form->get_config();

get_name()

Get the identifier of the fieldset instance.

静的 No
Parameters (none)
Returns string
Example
$article_form = Fieldset::forge('article');
$name = $article_form->get_name();  // 'article'

populate($input, $repopulate = false)

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

静的 No
パラメータ
パラメータ 規定値 説明
$input 必須 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
返り値 \Fieldset Object
$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).

静的 No
パラメータ None
返り値 \Fieldset Object
$article_form->repopulate();

build($action = null)

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

静的 No
パラメータ
パラメータ 規定値 説明
$action
null
A URL for the action attribute of the form.
返り値 string HTML markup
$this->template->form = $article_form->build(Uri::create('article/submit'));

disable($field)

The disable method allows you to disable a field from being build. You can use this if you want to alter the sequence of fields on the form, or to create more complex layouts without having to build individual fields.

静的 No
Parameters
Param Default Description
$field required The specific field you want to disable.
Returns \Fieldset Object
Throws RuntimeException, if the field given is not defined
Example
$fieldset->disable('name');

The field is not removed from the fieldset, so it will still be validated.

enable($field)

The enable method allows you to re-enable a field that has been disabled before.

静的 No
Parameters
Param Default Description
$field required The specific field you want to enable.
Returns \Fieldset Object
Throws RuntimeException, if the field given is not defined
Example
$fieldset->enable('name');

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.

静的 No
パラメータ
パラメータ 規定値 説明
$field
null
A specific field for which to get the input.
返り値 Array of input or string value of specified field
$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.

静的 No
パラメータ
パラメータ 規定値 説明
$field
null
A specific field for which to get the input.
返り値 Array of input or string value of specified field, or false if the field isn't found
$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.

静的 No
パラメータ
パラメータ 規定値 説明
$field
null
A specific field for which to get the input.
返り値 Array of input or string value of specified field, or false if the field isn't found
$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.

静的 No
パラメータ
パラメータ 規定値 説明
$config
array()
Uses keys open_list, close_list, open_error, close_error & no_errors. Overrides the values
返り値 Array of input or string value of specified field, or false if the field isn't found
$all_errors = $article_form->show_errors();

set_tabular_form($model, $relation, $parent, $blanks = 1)

Define an embedded fieldset on a related ORM model to create a One-to-Many form.

静的 No
Parameters
Param Default Description
$model required Fully namespaced name of the related model on which the form must be generated
$relation required Name of the relation of the parent object that relates to $model
$parent required Parent object that is used to generate the main form, and contains the related records to be displayed in the tabular form
$blanks
1
Number of empty rows to be displayed in the tabular form
Returns Fieldset, for chaining
Throws RuntimeException, if incorrect parameters are passed
Example See below...

The tabular form is displayed in a table, controlled by template definitions in the config/form.php configuration file. You are free to modify these templates, but you should leave the table structure, as the fieldset class will add the table headers based on the fields present in each row. This will generate invalid HTML if you remove the table and row definition from the template.

get_tabular_form()

Return the name of the defined tabular form.

静的 No
Parameters None
Returns mixed, the name of the form, or false if the fieldset is not a tabular form
Example
if ($name = $form->get_tabular_form())
{
	echo '$form is a tabular fieldset named', $name;
}

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.

Creating One-to-Many forms with ORM Models

The Fieldset class can also be used to create forms for an ORM model object and a Many relation. This is done by embedding a tabular form fieldset into the main form fieldset, specifying the related model and the name of the relation.

By default, the One-to-Many fieldset is displayed in tabular form, controlled by the template in the config/form.php file.

// We're going to create a one-to-many form of an article, identified by $id, and it's comments

// get the article and it's related comments
$article = Model_Article::find($id, array('related' => array('comments')));

// create the form fieldset for this article using the ORM model's form definition
$form = Fieldset::forge('article')->add_model($article);

/*
 * add the tabular form to it to create the one-to-many table.
 *
 * the data is provided by the relation 'comments' of the $article object, which
 * are objects of the class 'Model_Comments'.
 *
 * you can disable the fieldset tag to avoid embedded fieldsets in your form
 */
$form->add(\Fieldset::forge('tabular')->set_tabular_form('Model_Comments', 'comments', $article)->set_fieldset_tag(false));
				

This only works for related models that DO NOT have a compound key!

A tabular form generates HTML input tags that use an array with the name of the relation specified, and the primary key value of the record as it's index value. Any blank rows that are added at the bottom of the table will use the relation name, with a '_new' suffix. This will allow you to update the parent record, and all related records, in one go, and process the new lines separately.

In this example, you'll see input tag names like comments[12][fieldA] for existing rows (in this case a row with id = 12, and comments_new[0][fieldA] for a field in the first blank line.

// Process the posted one-to-many form
if (\Input::post())
{
	// validate the input
	$form->validation()->run();

	// if validated, save the updates
	if ( ! $form->validation()->error())
	{
		// update the article record, and any comment record changes
		$article->from_array($data);

		// do we need to delete tabular form records?
		foreach ($data['comments'] as $id => $row)
		{
			if ($id and ! empty($row['_delete']))
			{
				unset($article->comments[$id]);
			}
		}

		// do we have a new tabular form record data (assuming we've added only one blank line)?
		$data = array_filter($data['comments_new'][0]);

		$new_errors = false;
		if ( ! empty($data))
		{
			// check for required fields
			if (empty($data['fieldA']) or empty($data['fieldB']))
			{
				$new_errors = true;
				// display an error message about missing required fields here...
			}
			else
			{
				// create a new related record
				$article->comments[] = Model_Comments::forge(array('article_id' => $article->id, 'fieldA' => $data['fieldA'], 'fieldB' => $data['fieldB']));
			}
		}

		// save the updates
		if ( ! $new_errors)
		{
			if ($article->save())
			{
				// display a save-succesful message here and redirect away
			}
			else
			{
				// display an error message, save was not succesful
			}
		}
	}
	else
	{
		// inform the user validation failed
	}
}

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.