Validation Class
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.
The forge method returns a new Validation instance,
and links it with a Fieldset with name $fieldset.
Static |
Yes |
Parameters |
Param |
Type |
Default |
Description |
$fieldset |
string | Fieldset |
'default'
|
The name or instance of the Fieldset to link to. |
|
Returns |
Validation object |
Throws |
\DomainException, when the Fieldset name or instance already has a related Validation instance. |
Example |
$val = Validation::forge();
$val = Validation::forge('my_fieldset');
$fieldset = Fieldset::instance('my_fieldset');
$val = Validation::forge($fieldset);
|
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 |
Parameters |
Param |
Type |
Default |
Description |
$name |
string |
null
|
The name of the Fieldset whose Validation instance you want to return. |
|
Returns |
Validation object
or false if the specified Fieldset instance doesn't exist
|
Example |
$val = Validation::instance();
$val = Validation::instance('my_fieldset');
|
The active method returns the currently active validation instance.
Static |
Yes |
Parameters |
None
|
Returns |
Validation - currently active validation instance |
Example |
$val = Validation::active();
|
The active_field method returns the field currently being validated.
Static |
Yes |
Parameters |
None
|
Returns |
Fieldset_Field - the field currently being validated |
Example |
$field = Validation::active_field();
|
The fieldset method returns the related Fieldset.
Static |
No |
Parameters |
None
|
Returns |
Fieldset - the related Fieldset |
Example |
$val = Validation::forge('my_fieldset');
$fieldset = $val->fieldset();
|
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 |
Parameters |
Param |
Type |
Default |
Description |
$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.
|
|
Returns |
Fieldset_Field |
Example |
$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]');
$val->add_field('username', 'Username', array('required', 'trim', 'valid_string[alpha,lowercase,numeric]'));
|
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 |
Parameters |
Param |
Type |
Default |
Description |
$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. |
|
Returns |
Validation instance |
Example |
$val->set_message('required', 'You have to fill in your :label so you can proceed');
$val->set_message('required', null);
$val->set_message('custom_rule', 'The :label you entered is previously registered. Please choose another one');
|
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 |
Parameters |
Param |
Type |
Default |
Description |
$rule |
string |
None
|
Name of the rule of which you need to get the message. |
|
Returns |
string |
Example |
$val->set_message('custom_rule', 'The :label you entered is previously registered. Please choose another one');
$val->get_message('required', null);
|
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 |
Parameters |
Param |
Type |
Default |
Description |
$class |
string|Object |
None
|
Name of the class as string, or object name |
|
Returns |
Validation instance |
Example |
$val->add_callable('myvalidation');
Check the Extending the Validation class for a complete example of using this method, and the required class.
|
The remove_callable method removes a callable from the callables array.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$class |
string|Object |
None
|
Name of the class as string, or object name |
|
Returns |
Validation instance |
Example |
$val->remove_callable('myvalidation');
|
The callables method fetches the objects for which you don't need to add a full callback but just the method name
Static |
No |
Parameters |
None |
Returns |
array |
Runs the defined validation rules against the input passed, or Input::post() if nothing was passed.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$input |
array |
null
|
Assoc array of input fields and values |
$allow_partial |
bool |
false
|
If false, all fields that have validation rules defined MUST be present in the input |
$temp_callables |
array |
array()
|
Any additional callables required to run all defined validation rules |
|
Returns |
Boolean, true if the input validated, false if it failed |
Example |
$val = Validation::forge('article');
$val->add_field('title', 'Title', 'required');
if ( ! $val->run())
{
foreach ($val->error() as $field => $error)
{
echo $error->get_message();
}
}
|
Returns one or all input values, or the default value if the requested key is not present in the input.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$key |
string |
null
|
Name of the input field, or null to return all input |
$default |
mixed |
null
|
Value to return if the key is not present in the input |
|
Returns |
Mixed, the input value or the value specified as $default |
Example |
$val = Validation::forge('article');
$val->add_field('title', 'Title', 'required');
if ($val->run())
{
echo $val->input('title');
}
|
Input is only available after the validation rules have run.
By default, input() will fall back to Input::param() when no rules have run. This behaviour can be controlled through the validation config file.
Returns one or all validated input values, or the default value if the requested key is not present in the validated input.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$field |
string |
null
|
Name of the input field, or null to return all input |
$default |
mixed |
null
|
Value to return if the key is not present in the input |
|
Returns |
Mixed, the validated input value or the value specified as $default |
Example |
$val = Validation::forge('article');
$val->add_field('title', 'Title', 'required');
if ($val->run())
{
echo $val->validated('title');
}
|
Where input() returns the raw input value, validated() will return the validated value,
which might be different than the input because some validation rules alter the input (for example a rule like strtoupper).
The error method returns specific error or all errors that occur during validation.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$field |
string |
null
|
The fieldname of the Validation_Error instance you want to return. If you don't specify the fieldname, then returns all error objects. |
$default |
mixed |
false
|
If an error of the specified field does not exist, returns this value. |
|
Returns |
Validation_Error instance
or array if you don't specify the fieldname |
Example |
$val = Validation::forge('article');
$val->add_field('title', 'Title', 'required');
if ( ! $val->run(array()))
{
foreach ($val->error() as $field => $error)
{
echo $error->get_message();
}
}
|
The error_message method returns specific error message or all error messages that occur during validation.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$field |
string |
null
|
The fieldname of the error message you want to return. If you don't specify the fieldname, then returns all error messages. |
$default |
string |
false
|
If an error of the specified field does not exist, returns this value. |
|
Returns |
string
or array if you don't specify the fieldname |
Example |
$val = Validation::forge('article');
$val->add_field('title', 'Title', 'required');
if ( ! $val->run(array()))
{
foreach ($val->error_message() as $field => $message)
{
echo $message;
}
}
|
The show_errors returns the list of validation errors rendered in HTML according to the defined template.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$options |
array |
array()
|
Options for the HTML template, to override the defaults from the validation config file |
|
Returns |
string, the rendered HTML |
Example |
$val = Validation::forge('article');
$val->add_field('title', 'Title', 'required');
if ( ! $val->run(array()))
{
echo $val->show_errors();
}
|
The following options can be passed: 'open_list', 'close_list', 'open_error', 'close_error' and 'no_errors'. By default
an unordered list is generated, and an empty string is returned if no validation errors were found.
Alias for the Fieldset add() method.
Alias for the Fieldset add_model() method.
Alias for the Fieldset field() method.