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.

Error messages

Errors are returned as Validation_Error objects which you can work with for more flexible output, if you don't need that just cast the Validation_Error object to a string to get the error message.

Error messages are set using a language file "validation.php" which is loaded automatically.

There are 2 ways of manipulating the error messages during validation.

// change the error message for a specific validation object
$val->set_message('required', 'The field :label is required.');

// or change the message for a specific field, no matter the actual error
echo $val->error('username')->get_message('The field :label must be filled out before auth is attempted.');
// Outputs "The field Your username must be filled out before auth is attempted." when validation for username
// failed, or nothing when not yet validated or validation succeeded.

As you may have noticed the error messages accept a couple of variables that are replaced by the field's properties. Below is a list of the available variables:

Variable 説明
:field Will be substituted by the field's name.
:label Will be substituted by the field's label.
:value Will be substituted by the field's value which could not be validated.
:rule Will be substituted by the rule that failed. The included rules will be just their name, others will be the string callback or classname:method (which will also be the key for which you must add a validation rule if you want to).
:param:1 Will be replaced by the first additional parameter's value, :param:2 will be the second, etc.

Naming callback/closure rules

Full callback rules or closures can be named as well, by default their names will be:

  • Closure: "closure"
  • array($class, $method): "$class:$method"
  • array($object, $method): get_class($object).":$method"

If you want to give them custom names instead you can do that like this:

// Add a rule which checks if the input is odd
// It can either use ->set_message('odd', ':label is not odd.') or use a lang key 'validation.odd'
$field->add_rule(array('odd' => function($val) { return (bool) ($val % 2); }));

Validation Error objects

Validation returns error objects which are actually exceptions thrown during validation. These objects can be cast to string in order to get their actual message or be used as objects to fetch metadata about the error.

$field

The $error->field contains a reference to the Fieldset_Field object that caused this error.

$value

$error->value contains the value that failed to validate.

$rule

$error->rule contains the name of the validation rule that failed. For most rules this will be the string that was passed as the validation rule. It can however also be another string when specificly named.
For callbacks passed like 'Class::method', array('Class', 'method') or array($object, 'method') it will be a simplified form: 'Classname:methodname'.

$params

This will contain any additional parameters passed to the validation rule. For example when using ->add_rule('max_length', 25) this will contain array(25).

get_message($msg = false, $open = '', $close = '')

Fetches the error message and allows rewriting it.

Static No
パラメータ
パラメータ 規定値 説明
$msg false | string
false
Will attempt to fetch the message from the Validation instance it belongs to or from config when false was given. Will return a default placeholde message on empty input or when there was no message to be found.
Or it will use the given input as the message and replace the variables in it.
$open string
''
Adds HTML before the message.
$close string
''
Adds HTML after the message.
返り値 string
// Fetch the message inside <p> tags
	$message = $error->get_message(false, '<p>', '</p>');

	// Will use a standard message: Validation rule :rule failed for :label
	$message = $error->get_message(null);

	// Overwrite whetever error message might have been given
	$message = $error->get_message('Validation failed for field :label.');