Note: This is the Validation class from Laravel with slight modifications.
$validator = Validator::make(
array('username' => 'dayle'),
array('username' => 'required|alpha_dash|min:3')
);
The first argument passed to the make method is the data under validation. The second argument is the validation rules that should be applied to the data.
$validator = Validator::make(
array(
'username' => 'dayle',
'password' => 'mypassword',
'email' => 'email@example.com'
),
array(
'name' => 'required',
'password' => 'required|between:4,30',
'email' => 'required|email|unique:users'
)
);
Once a Validator instance has been created, the fails (or passes) method may be used to perform the validation.
if ($validator->fails())
{
// The given data did not pass validation
}
If validation has failed, you may retrieve the error messages from the validator.
$messages = $validator->messages();
You may also access an array of the failed validation rules, without messages. To do so, use the failed method:
$failed = $validator->failed();
After calling the messages method on a Validator instance, you will receive a MessageBag instance, which has a variety of convenient methods for working with error messages.
echo $messages->first('email');
foreach ($messages->get('email') as $message)
{
//
}
foreach ($messages->all() as $message)
{
//
}
if ($messages->has('email'))
{
//
}
echo $messages->first('email', '<p>:message</p>');
foreach ($messages->all('<li>:message</li>') as $message)
{
//
}
Below is a list of all available validation rules and their function:
The field under validation must be yes, on, or 1. This is useful for validating "Terms of Service" acceptance.
The field under validation must be a valid URL according to the checkdnsrr PHP function.
The field under validation must be a value after a given date. The dates will be passed into the PHP strtotime function.
The field under validation must be entirely alphabetic characters.
The field under validation may have alpha-numeric characters, as well as dashes and underscores.
The field under validation must be entirely alpha-numeric characters.
The field under validation must be of type array.
The field under validation must be a value preceding the given date. The dates will be passed into the PHP strtotime function.
The field under validation must have a size between the given min and max. Strings, numerics, and files are evaluated in the same fashion as the size rule.
The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.
The field under validation must be a valid date according to the strtotime PHP function.
The field under validation must match the format defined according to the date_parse_from_format PHP function.
The given field must be different than the field under validation.
The field under validation must be numeric and must have an exact length of value.
The field under validation must have a length between the given min and max.
The field under validation must be able to be cast as a boolean. Accepted input are true, false, 1, 0, "1" and "0".
The field under validation must be formatted as an e-mail address.
The field under validation must exist on a given database table.
'email' => 'exists:users'
'email' => 'exists:users,email_address'
You may also specify more conditions that will be added as "where" clauses to the query:
'email' => 'exists:users,email,id,1'
Passing NULL as a "where" clause value will add a check for a NULL database value:
'email' => 'exists:users,email,display_name,NULL'
The file under validation must be an image (jpeg, png, bmp, or gif)
The field under validation must be included in the given list of values.
The field under validation must have an integer value.
The field under validation must be formatted as an IP address.
The field under validation must be less than or equal to a maximum value. Strings, numerics, and files are evaluated in the same fashion as the size rule.
The field under validation must have a minimum value. Strings, numerics, and files are evaluated in the same fashion as the size rule.
The field under validation must not be included in the given list of values.
The field under validation must have a numeric value.
The field under validation must match the given regular expression.
Note: When using the regex pattern, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character.
The field under validation must be present in the input data.
The field under validation must be present if the field field is equal to value.
The field under validation must be present only if any of the other specified fields are present.
The field under validation must be present only if all of the other specified fields are present.
The field under validation must be present only when any of the other specified fields are not present.
The field under validation must be present only when the all of the other specified fields are not present.
The given field must match the field under validation.
The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value. For files, size corresponds to the file size in kilobytes.
The field under validation must be a valid timezone identifier according to the timezone_identifiers_list PHP function.
The field under validation must be unique on a given database table. If the column option is not specified, the field name will be used.
'email' => 'unique:users'
'email' => 'unique:users,email_address'
'email' => 'unique:users,email_address,10'
You may also specify more conditions that will be added as "where" clauses to the query:
'email' => 'unique:users,email_address,NULL,id,account_id,1'
In the rule above, only rows with an account_id of 1 would be included in the unique check.
The field under validation must be formatted as an URL.
If needed, you may use custom error messages for validation instead of the defaults. There are several ways to specify custom messages.
$messages = array(
'required' => 'The :attribute field is required.',
);
$validator = Validator::make($input, $rules, $messages);
Note: The
:attributeplace-holder will be replaced by the actual name of the field under validation. You may also utilize other place-holders in validation messages.
$messages = array(
'same' => 'The :attribute and :other must match.',
'size' => 'The :attribute must be exactly :size.',
'between' => 'The :attribute must be between :min - :max.',
'in' => 'The :attribute must be one of the following types: :values',
);
Sometimes you may wish to specify a custom error messages only for a specific field:
$messages = array(
'email.required' => 'We need to know your e-mail address!',
);
In some cases, you may wish to specify your custom messages in a language file instead of passing them directly to the Validator. To do so, add your messages to custom array in the app/lang/xx/validation.php language file.
'custom' => array(
'email' => array(
'required' => 'We need to know your e-mail address!',
),
),
To specify some custom validation rules use the Validator::extend method:
Validator::extend('foo', function($attribute, $value, $parameters)
{
return $value == 'foo';
});
The custom validator Closure receives three arguments: the name of the $attribute being validated, the $value of the attribute, and an array of $parameters passed to the rule.