This issue recommends a PHP form validation solution called Microengine form validation.
Form verification is an indispensable part of every website, such as login judgment, submit information, query information, feedback information, etc. It is a bridge between the website manager and the viewer. Microengine provides a more convenient, fast, and secure extension that meets almost all current verification needs.
Microengine based on Laravel made the following extensions:
- A validator can be defined as a class
- Add verification scenarios
- Added rule manager
- Add a data default value
- Add data filter
- Add scene event
- The user-defined authentication rule is modified
- Custom messages add references to content
- The inheritance collection class adds a validation collection
The validator supports Laravel’s built-in rules, the built-in rules document can be viewed:
Form verification | Laravel 7 Chinese Documentation 7.x | Laravel China community
example:
1、Simple verification
Support simply define a validator and Validate, if the validation passes, return all validated values, if not, throw a W7\Validate\Exception\ValidateException exception
try {$data = Validate::make(['user' => 'required|email','pass' => 'required|lengthBetween:6,16',], ['user.required' => 'Please enter your username','user.email' => 'The user name format is incorrect','pass.required' => 'enter your PIN','pass.lengthBetween' => 'The password contains 6 to 16 characters',])->check($data);} catch (ValidateException $e) {echo $e->getMessage();}
2、Validator definition
To define a validator class for a specific validation scenario or data form, we need to inherit the W7\Validate\Validate class, and then directly call the check method of the validation class after instantiation to complete the validation
class LoginValidate extends Validate{protected $rule = ['user' => 'required|email','pass' => 'required|digits_between:6,16',];protected $message = ['user.required' => 'Please enter your username','user.email' => 'The user name format is incorrect','pass.required' => 'enter your PIN','pass.digits_between' => 'The password contains 6 to 16 characters',];}
3、data validation
$data = ['user' => '123@qq.com','pass' => ''];$validate = new LoginValidate();$validate->check($data);
A W7\Validate\Exception\ValidateException will be thrown with message please enter a password
$data = ['user' => '123@qq.com','pass' => '123456'];$validate = new LoginValidate();$data = $validate->check($data);
The verification succeeds, and the value that passes the verification is returned. The returned value is of the array type.
Read more on your own.