The example in this article describes the validator validation extension usage in Laravel4. Share to everyone for your reference, specific as follows:
Whether you write the interface or write a Web page, the essence is passed in the parameters, and then the business logic, and then output the specific content. Therefore, the verification of the parameters is an inevitable link, such as the transmission over the email is not empty, is not a legitimate email format? Laravel has come up with a way to simplify this logic for phper. Is validator.
The use of validator
Create a Validator
Validator use Validator::make to create a validator. Then use the validator to determine if the incoming arguments are legitimate.
See the definition of Make:
Copy Code code as follows:
Public Validator make (array $data, array $rules, array $messages = Array (), array $customAttributes = Array ())
$data is the data to be validated by keyvalue
$rules is the rule of validation
$messages is the cue message
$customAttributes is the property alias setting.
The latter two messages are set to prompt for information
Take a direct look at an example, understand the validator on the understanding:
Public Function GetIndex ()
{
$rules = array (
' email ' => ' required|email ', '
name ' => ' required| between:1,20 ',
' password ' => ' Required|min:8 ',
);
$message = Array (
"required" => ": attribute cannot be empty",
"between" => ": attribute length must be in: Min and: Max"
);
$attributes = Array (
"email" => ' e-mail ', '
name ' => ' username ',
' password ' => ' user password '
);
$validator = Validator::make (
input::all (),
$rules,
$message,
$attributes
);
if ($validator->fails ()) {
$warnings = $validator->messages ();
$show _warning = $warnings->first ();
Return Response::json (Compact (' show_warning '));
}
Return Response::json ("OK");
}
In this case,
If you pass the argument to
/index?name=12321321321321321321321
will return:
{
show_warning: "e-mail cannot be empty"
}
Input::all () passes all parameters in for validation
Rule stipulates that the email field can not be empty, it must conform to the format of the email. Rule stipulates that the email field can not be empty, it must conform to the format of the email. The message indicates that if the required rule is violated, the format for displaying the error is: ": attribute cannot be empty"
$attributes indicate that the error shown in email appears as text "e-mail"
The above settings add up to cause the last parameter not to send email when the error message displayed is:
"E-mail cannot be empty"
Verification Extension for Validator
Validation Rule Extension
You will encounter such as to verify whether the phone number is legitimate, Laravel's validator did not provide the rules of cell phone number verification, because each country's mobile phone number rules are not uniform.
But what about the use of validator for verification?
Validator::extend (' Mobile ', function ($attribute, $value, $parameters)
{return
preg_match ('/^0? 13[0-9]|15[012356789]|18[0-9]|14[57]) [0-9]{8}$/', $value);
});
And then you can use mobile as the rule.
Where does this extended validation rule go?
I suggest adding a validator.php in the filters sibling directory and then start/global.php
Require App_path (). ' /validator.php ';
Uniform Tips for output information
In the example above, message and message and attribute all need to use the validator of their own definition, more trouble, there is no better way to unify the settings?
The prompt settings for validator are set according to the language.
The language setting is defined in the app.php inside the Config directory locale. The default is en.
and EN's corresponding error prompts are set in the validation.php in the En directory in the lang directory.
You can take a look at the lang/en/validation.php to understand how the corresponding English hint information came out.
To set the Chinese hint:
① Modify the locale inside the config/app.php, set to CH
② Create lang/ch/validation.php
③ Modify the validation.php file, will be inside the hint information modified to Chinese, note the message: attribute in the display will be replaced by the corresponding property name, attributes is set properties of the Chinese name.
From here I can see what laravel support for Cross-language.
The folders under Lang include pagination in addition to validation.php: pagination.php, Hint: reminders.php file.
What rule rules do laravel bring?
Check the brochure ... Laravel validation rules for self-band
More interested in laravel related content readers can view the site topics: "Laravel Framework Introduction and Advanced Course", "PHP Excellent Development Framework Summary", "Smarty Template Primer Tutorial", "PHP date and Time usage summary", "PHP object-oriented Program Design Introductory Course ", PHP string (String) Usage summary," PHP+MYSQL Database operation Introduction Tutorial "and" PHP common database Operation Skills Summary "
I hope this article will help you with the PHP program design based on Laravel framework.