Laravel 5.5 Custom Validation Object/class

Source: Internet
Author: User

This article and we share the main is Laravel 5.5the custom Validation object/classRelated content, come together to look at it, hope to everyone learn laravel helpful. Laravel 5.5a new custom validation rule object will be provided as the originalValidator::extendalternative to the method. Laravelforms validation is convenient, and there are a number of available validation rules built in, but no matter what the official offer, there is always a time to meet the demand. Most of the time we use regular expressions to deal with this particular validation, and sometimes we choose to useValidator::extendto extend a custom rule. But inLaravel 5.5version, we have a new approach, as long as we define an implementationIlluminate\contracts\validation\rule interface to implement custom validation rules and can be used directly. The following is a simple example: UseIlluminate\Contracts\Validation\Rule; classIsoddvalidationruleImplementsRule{ Publicfunctionpasses($attributes, $value) { return($value% 2!== 0); } Publicfunctionmessage() { return': Attributemust be an odd number'; } }The code above defines a Isoddvalidationrulecustom validation class, in theControllerin order to use this validation class, you can write: PublicfunctionHandlform(Request $request) { $this->validate ($request, [ ' Oddfield ' = [NewIsoddvalidationrule] ]); }the same effect can also be achieved by an anonymous function (the closure function): PublicfunctionHandleform(Request $request) { $this->validate ($request, [ ' Oddfield ' = [function($attributes, $value, $fail) { if($value% 2 = = = 0) { $fail (': attributemust be an odd number! '); } }] ]); }The corresponding custom validation rule does not occur when the validated form item is NULL or does not exist. This is consistent with the logic of the system's own validation rules. If you want your custom validation rules to be executed even if the corresponding table item is null, then simply inherit the interface from theRuleChange intoImplicitrulecan: classIsoddvalidationruleImplementsImplicitrule{ ... }AdoptLaravel 5.5new custom validation classes to better manage a large number of custom validation rules, andPhpstorm or something.Ide, it is also more convenient to quickly jump from the validation code to the corresponding validation class code. After all, useValidator::extend, you can only find the source code for the rule definition by searching for a string that corresponds to the validation class name. The custom validation rules for anonymous functions can be very handy in a one-time simple validation logic, or it can be useful to quickly test the validation logic during coding. In general, however, it is recommended that you use custom validation classes that are more organized and readable. The best approach is to writeControllerthe process of using anonymous functions to quickly validate a custom rule and then move it to a custom validation class object. Source:cutting knife under the paper

Laravel 5.5 Custom Validation Object/class

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.