get_message($msg = false, $open = '', $close = '')
Fetches the error message and allows rewriting it.
Static | No | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
パラメータ |
|
||||||||||||||||
返り値 | string | ||||||||||||||||
例 |
|
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.
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. |
Full callback rules or closures can be named as well, by default their names will be:
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 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.
The $error->field
contains a reference to the Fieldset_Field
object that
caused this error.
$error->value
contains the value that failed to validate.
$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'
.
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)
.
Fetches the error message and allows rewriting it.
Static | No | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
パラメータ |
|
||||||||||||||||
返り値 | string | ||||||||||||||||
例 |
|