Detailed description of the User-Defined authentication object/class sample code of Laravel 5.5, laravel sample code

Source: Internet
Author: User

Detailed description of the User-Defined authentication object/class sample code of Laravel 5.5, laravel sample code

Laravel 5.5 will provide a brand new custom validation rule object to replace the original Validator: extend method.

Laravel 5.5 will provide a brand new custom validation rule object to replace the original Validator: extend method .. In many cases, we use regular expressions to process such special verification, and sometimes we choose to use Validator: extend to extend a custom rule. However, in Laravel 5.5, we have a new method to implement custom verification rules by defining a class that implements the Illuminate \ Contracts \ Validation \ Rule interface, and can be used directly.

The following is a simple example:

Use Illuminate \ Contracts \ Validation \ Rule; class IsOddValidationRule implements Rule {public function passes ($ attributes, $ value) {return ($ value % 2! = 0);} public function message () {return ': attribute must be an odd number ';}}

The code above defines a custom verification class of IsOddValidationRule. To use this verification class in the Controller, you can write it like this:

public function handlForm(Request $request){  $this->validate($request, [    'oddField' => [new IsOddValidationRule]  ]);}

The same effect can also be achieved through anonymous functions (Closure functions:

Public function handleForm (Request $ request) {$ this-> validate ($ request, ['ddfield' => [function ($ attributes, $ value, $ fail) {if ($ value % 2 = 0) {$ fail (': attribute must be an odd number! ') ;}}]);}

When the form item for verification is null or does not exist, the corresponding custom verification rule is not executed. This is consistent with the logic of the built-in verification rules. If you want your custom validation rules to be executed even when the corresponding form item is null, you just need to change the inherited interface from rule to ImplicitRule:

class IsOddValidationRule implements ImplicitRule{  ...}

Using the custom verification class added by Laravel 5.5, you can better manage a large number of custom verification rules, and in IDE such as PHPStorm, it is also more convenient to quickly jump to the corresponding verification class code from the verification code. After all, if Validator: extend is used, you can only find the source code defined by the rule by searching the string corresponding to the verification class name.

The custom verification rules of anonymous functions can be used easily in a one-time simple verification logic, or it is also useful to quickly test the verification logic during the encoding process. However, we recommend that you use custom verification classes that are more organized and readable. The best way is to use an anonymous function to quickly authenticate a custom rule during Controller writing, and then move it to a custom validation class object.

You can view the Pull Request of this function on github of Laravel framework and read the specific implementation code and related test code.

Reference

Https://github.com/laravel/framework/pull/19155/files
Https://laravel-news.com/custom-validation-rule-objects

Summary

The above is a detailed explanation of the User-Defined verification object/class sample code of Laravel 5.5 introduced to everyone. I hope it will be helpful to everyone. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.