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.

Static Yes
パラメータ
パラメータ 規定値 説明
$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).

Static Yes
パラメータ
パラメータ 規定値 説明
$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.

Static 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.

Static 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.

Static 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.

Static 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.

Static 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.

Static 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.

Static 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.

Static 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.

Static 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();

populate($input, $repopulate = false)

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

Static 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).

Static 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.

Static 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'));

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
パラメータ
パラメータ 規定値 説明
$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.

Static 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.

Static 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.

Static 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
$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.