Validation クラス

The Validation class helps you validate user input, if you want to create a form & its validation at the same time use the Fieldset class instead.

Class Methods

forge($fieldset = 'default')

The forge method returns a new Validation instance, and links it with a Fieldset with name $fieldset.

Static Yes
パラメータ
パラメータ 規定値 説明
$fieldset string | Fieldset
'default'
The name or instance of the Fieldset to link to.
返り値 Validation object
例外 \DomainException, when the Fieldset name or instance already has a related Validation instance.
// Create a new validation instance
$val = Validation::forge();

// Associate the new Validation instance with a Fieldset, using the fieldset name
$val = Validation::forge('my_fieldset');

// Associate the new Validation instance with a Fieldset, by passing the fieldset instance
$fieldset = Fieldset::instance('my_fieldset');
$val = Validation::forge($fieldset);

instance($name = null)

The instance method returns the Validation instance related with the Fieldset instance with the identifier $name, or the default Fieldset (is created if necessary).

Static Yes
パラメータ
パラメータ 規定値 説明
$name string
null
The name of the Fieldset whose Validation instance you want to return.
返り値 Validation object
or false if the specified Fieldset instance doesn't exist
// Get the Validation instance of the default Fieldset
$val = Validation::instance();

// Get the Validation instance of a particular Fieldset
$val = Validation::instance('my_fieldset');

active()

The active method returns the currently active validation instance.

Static Yes
パラメータ None
返り値 Validation - currently active validation instance
// Get the currently active validation instance
$val = Validation::active();

active_field()

The active_field method returns the field currently being validated.

Static Yes
パラメータ None
返り値 Fieldset_Field - the field currently being validated
// Get the field currently being validated
$field = Validation::active_field();

fieldset()

The fieldset method returns the related Fieldset.

Static No
パラメータ None
返り値 Fieldset - the related Fieldset
// Get the related Fieldset
$val = Validation::forge('my_fieldset');
$fieldset = $val->fieldset();

add_field($name, $label, $rules)

The add_field method is a simpler alias for add() method which allows specifying the field name, label and rules in a single step.

Static No
パラメータ
パラメータ 規定値 説明
$name string
None
The field name to assign the validation rule to.
$label string
None
Label of the field.
$rules mixed
None
The set of validation rules with (optional) rule parameters grouped in square brackets, and separated with commas. It can be passed as a string where the rules are separated with a vertical bar or pipe symbol (|), or as a array of rules.
返り値 Fieldset_Field
// Set rules on different types of fields
$val->add_field('username', 'Username', 'required|trim|valid_string[alpha,lowercase,numeric]');
$val->add_field('email', 'Email', 'required|trim|valid_email');
$val->add_field('age', 'Age', 'valid_string[numeric]');

// as an array
$val->add_field('username', 'Username', array('required', 'trim', 'valid_string[alpha,lowercase,numeric]'));

set_message($rule, $message)

The set_message method overwrites the language file messages for this validation instance. It's also useful when no message is assigned to the rule you're using.

Static No
パラメータ
パラメータ 規定値 説明
$rule string
None
Name of the rule to assign this message to.
$message string | null
None
The message to display for this rule, if a string is passed, or null to revert to the language file.
返り値 Validation instance
// Overwrite the required rule message
$val->set_message('required', 'You have to fill in your :label so you can proceed');
// .. when done, set back the default message
$val->set_message('required', null);
// A message for a custom rule you created
$val->set_message('custom_rule', 'The :label you entered is previously registered. Please choose another one');

get_message($rule)

The get_message method fetches a specific error message for this validation instance. Only messages set through the set_message method are returned.

Static No
パラメータ
パラメータ 規定値 説明
$rule string
None
Name of the rule of which you need to get the message.
返り値 string
// Overwrite the required rule message
$val->set_message('custom_rule', 'The :label you entered is previously registered. Please choose another one');
// .. later, or some where else in your code, get this message
$val->get_message('required', null);

add_callable($class)

The add_callable method adds a set of custom or extended validation rules. You don't need to write a full callback, just the class name as a string will do. This also allows for overwriting functionality from this object because the new class is prepended.

Static No
パラメータ
パラメータ 規定値 説明
$class string|Object
None
Name of the class as string, or object name
返り値 Validation instance

$val->add_callable('myvalidation');

Check the Extending the Validation class for a complete example of using this method, and the required class.

remove_callable($class)

The remove_callable method removes a callable from the callables array.

Static No
パラメータ
パラメータ 規定値 説明
$class string|Object
None
Name of the class as string, or object name
返り値 Validation instance
$val->remove_callable('myvalidation');

callables()

The callables method fetches the objects for which you don't need to add a full callback but just the method name

Static No
パラメータ None
返り値 array

More coming soon...