thinkphp5.0 Custom Validator

Source: Internet
Author: User

Although I would have some PHP basic grammar, I set the data, I wrote the controller layer, not using the API way, the company is to exercise me, so just do, basically things are with the framework of their own, and do not have to encapsulate these things, so the programming ideology, but also very deficient, So I have to imitate someone else's code first.

Problem:

 How to customize the validator class, and how to use custom rules;

Whether it is a validator, or interceptors, filters, are to inherit the corresponding class, as long as the inheritance is the corresponding to what validator, interceptor ... The

eg

1. Custom Validator Classes

Class Validatecustom extends Validate
{
Write the method name directly in the back;
protected $rule = [
' id ' = ' require| Ispositiveinteger'
];


/**
* Determine if a number is a positive integer
*/
Ispositiveinteger ($value, $rule = ", $data =", $field = ")
{

if (Is_numeric ($value) && is_int ($value + 0) && ($value + 0) > 0) {
return true;
} else {
Return $field. ' must be a positive integer ';
}
}

}

2. Use of other classes
$data = [
' id ' = $id
];
Validator
$validate = new Validatecustom ();
Perform batch Validation
$result = $validate->batch ()->check ($data);
Output Error Hints
Var_dump ($validate->geterror ());

thinkphp5.0 Custom Validator

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.