The following brings you an example of automatic verification of the create() method in ThinkPHP. The content is quite good, so I will share it with you now and give it as a reference.
Automatic verification is a data verification method provided by the ThinkPHP model layer, which can automatically perform data verification when using create to create a data object.
Principle:
##The create() method collects form ($_POST) information and returns it, while triggering automatic verification of the form and filtering illegal fields,
Using the create() method in the controller, (the return value is true/false), will automatically trigger the $_validate attribute in the model class (it is the method in the parent class Model, in the subclass Rewritten in Model), customize the verification rules in $_validate (the verification rules will be explained in detail below), when the create() method has no data and the return value is false, obtain and Return error message!
Using automatic verification must be defined according to the following rule format:
protected $_validate = array( array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]), array(验证字段2,验证规则,错误提示,[验证条件,附加规则,验证时间]), ...... );
Validation field (required): form field.
Validation Rules (required): The require field must include email address, url URL address, and number. It can also be used in conjunction with additional rules.
Error prompt (required): Prompt message returned when verification fails.
Verification conditions (optional): There are three types: 0, 1, and 2. 0: Field verification that exists in _POST, default; 1: Verification is required if the verification rules are defined; 2: When the value is not empty Verification.Additional rules:
Regular verification, defined The validation rule is a regular expression (default) | |
Function verification, the defined validation rule is a function name | |
Method verification, the defined verification rule is a method of the current model class | |
Verify whether the two fields in the form Same, the defined validation rule is a field name | |
to verify whether it is equal to a certain value, which value is defined by the previous validation rule | |
Verify whether it is not equal to a certain value, which is defined by the previous validation rule (new in version 3.1.2) | |
Verify whether it is within a certain range. The defined verification rule can be an array or a comma-separated string. | |
Verify whether it is not within a certain range. Within a certain range, the defined verification rule can be an array or a comma-separated string (new in version 3.1.2) | |
Verification length, defined The validation rule can be a number (representing a fixed length) or a range of numbers (for example, 3,12 represents a range of length from 3 to 12) | |
Validation range , the defined verification rules represent the range, and you can use strings or arrays, such as 1,31 or array(1,31) | |
Verification is not within a certain range , the defined verification rules represent the range, you can use strings or arrays (new in version 3.1.2) | |
Verify whether it is within the validity period, the defined verification rules Indicates the time range, which can be up to the time. For example, you can use 2012-1-15, 2013-1-15 to indicate that the current submission validity period is between 2012-1-15 and 2013-1-15. You can also use the timestamp definition | |
Verify whether the IP is allowed. The defined verification rules represent a list of allowed IP addresses, separated by commas, such as 201.12.2.5,201.12.2.6 | |
Verify whether the IP is banned. The defined verification rules represent a list of banned IP addresses, separated by commas, such as 201.12.2.5,201.12.2.6 | |
To verify whether it is unique, the system will query the database based on the current value of the field to determine whether the same value exists. When the form data contains the primary key field, unique cannot be used to determine the primary key field itself |