Home >Backend Development >PHP Tutorial >thinkPHP implements automatic form verification, thinkphp implements forms_PHP tutorial
Last night our boss asked me to work on the form automatic verification function. I spent a long time worrying about it and learned a lot of knowledge from the official website before I came up with it. Anyway, let me share my own results!
thinkphp defines automatic verification functions and regular expressions for us in the Model base class. We only need to establish the $_validate attribute under the model class of the corresponding database table.
1. We find the Model base class and you can see protected $_validate = array(); // Automatically verify that it is of array type. Define it in the corresponding data model file below;
2,
if (md5($verify) != Session::get('verify')) return false;
return true;
}
//Automatic verification
3. Format description: array (validation fields, validation rules, error prompts, validation conditions, additional rules, validation time),
4. Parameter explanation:
Verification field: The name of the form field that needs to be verified. This field is not necessarily a database field, but can also be some auxiliary fields of the form, such as confirming passwords and verification codes, etc.
Verification rules: The rules for verification need to be combined with additional rules (required). The official rules included are as follows (you can also add them yourself):
Prompt message: Definition of prompt message after verification failure (required),
Verification conditions: (optional)
There are three rules for verifying conditions:
Model::EXISTS_TO_VAILIDATE or 0, verify if the field exists (default)
Model::MUST_TO_VALIDATE or 1 must be verified
Model::VALUE_TO_VAILIDATE or 2 Validate when the value is not empty
Additional rules:
regex regular verification, indicating that the previous verification rule is a regular expression;
function uses function verification, indicating that the previous verification is a function name;
callback uses method verification, indicating that the verification rule is a method of the Model class;
confirm verifies whether the two fields in the form are equal. The verification rule is a field name;
equal verifies whether it is equal to a certain value. This value is defined by the previous verification rules;
in verifies whether it is within a certain range. The previous definition is an array;
unique verifies whether it is unique. The system will query the database based on the current value of the field to determine if the same value exists;
At the same time, the system also has built-in some commonly used regular verification rules, which can be used in this section, including: require field must be email;
currency currency, number number, these verification rules can be used directly;
Verification time: (optional)
01.Model:: MODEL_INSERT or verify when adding new data
02.Model:: MODEL_UPDATE or 2 verify when editing data
03.Model:: MODEL_BOTH or 3 Verify in all cases (default)
5. Official example:
I don’t know if it meets our boss’s requirements, but it’s still possible. It’s so hard! ! I spent the whole night looking for codes, cases, and comfort! !