In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to customize verification objects in Laravel5.5". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to customize validation objects in Laravel5.5"!
Laravel 5.5 will provide an entirely new object for custom validation rules as an alternative to the original Validator::extend method. Many times we will directly use regular expressions to handle this special validation, and sometimes we will choose to use Validator::extend to extend a custom rule. However, in Laravel 5.5, we have a new means to implement custom validation rules by defining a class that implements the Illuminate\ Contracts\ Validation\ Rule interface, and can be used directly.
Here 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 odd';}}
The above code defines a custom validation class for IsOddValidationRule. If you want to use this validation class in Controller, you can write:
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, ['oddField' = > [function ($attributes, $value, $fail) {if ($value% 2 = 0) {$fail (': attribute must be odd!') ;}}]]);}
When the validated form item is null or does not exist, the corresponding custom validation rule is not executed. This is consistent with the logic of the validation rules that come with the system. If you want your custom validation rules to be executed even if the corresponding form item is null, simply change the inherited interface from rule to ImplicitRule:
Class IsOddValidationRule implements ImplicitRule {...}
Using the new custom validation class in Laravel 5.5, you can better manage a large number of custom validation rules, and in IDE such as PHPStorm, it is more convenient to quickly jump from the validation code to the corresponding validation class code. After all, with Validator::extend, you can only find the source code of the rule definition by searching for a string corresponding to the name of the validation class.
Custom validation rules for anonymous functions are really convenient to use in one-off simple validation logic, or it is also useful to quickly test validation logic during coding. In general, however, custom validation classes that are more organized and readable are recommended. The best approach is to quickly validate the custom rule with anonymous functions while writing Controller, and then move it to a custom validation class object.
At this point, I believe you have a deeper understanding of "how to customize the verification object in Laravel5.5". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.