The format of ThinkPHP automatic verification is as follows (for more information, see ThinkPHP automatic verification and verification rules): review of the ThinkPHP automatic verification syntax
The format of ThinkPHP automatic verification is as follows (for more information, see ThinkPHP
Detailed description of automatic verification and verification rules):
Array (verification field, verification rule, error prompt [, verification condition] [, additional rule] [, verification time])
ThinkPHP
The additional rules defined for automatic verification are as follows:
Regex: use regular expressions for verification (default)
Unique: verify uniqueness
Confirm: verify that the two fields in the form are the same
Equal: verify whether it is equal to a value
In: verify if it is within a certain range
Function: use function verification
Callback: use method verification
Automatic Verification example
Examples of automatic verification are as follows:
- // Use regular expressions for verification by default
- Array ('title', 'require ', 'title cannot be blank. '),
- Array ('order', 'number', 'Order must be a number. ', 2 ),
- Array ('email ', 'Email', 'mailbox format does not meet the requirements. '),
- Array ('QQ', 'QQ', 'qq number is incorrect. '),
- // Verify that the title field is unique when adding a title
- Array ('title', '', 'the title already exists! ', 0, 'Unique', 1 ),
- // Verify that the password is consistent with the password
- Array ('repassword', 'password', 'confirm that the password is incorrect. ', 0, 'confirm '),
- // Verify that the value of the class is one class.
- Array ('class', 'class 1 ', 'class 1 is required. ', 0, 'equal '),
- // When the value is not empty, determine whether the value is within the specified range.
- Array ('value', array (, 3), 'value range is incorrect. ', 2, 'in '),
- // Customize the username format for function verification
- Array ('username', 'checkname', 'username format is incorrect. ', 0, 'Function '),
- // When registering or changing the information, call the checkEmail method to check the email address.
- Array ('email ', 'checkemail', 1, 'callback '),
Use regular expression (regex) for verification
In the above several types of additional rules, regular expressions are often used and are also the default verification additional rules. The system has the following built-in regular expression detection rules:
Require (required), email (email format), url (URL address), currency (currency), number (number), qq (qq number), english (english character ).
These additional rules can be used directly. if these additional rules cannot meet the requirements, you can use custom regular rules:
Array ('username', '/^ {3, 15} $/', 'user name does not meet the requirements. '),
This rule requires that the user name can only contain English characters, underscores, and numbers, and the length is 3-15 bytes.
For more information about regular expression rules, see
Sort regular expressions.
Use a custom function for verification
Use a custom function to verify the additional rules. The function can be Common/common. php.
You can also use PHP built-in functions:
- Class UserModel extends Model {
- Protected $ _ validate = array (
- Array ('username', 'checkname', 'username does not meet the requirements. ', 0, 'Function '),
- };
- }
Custom checkName
Function:
- Function checkName ($ username ){
- If (! Preg_match ('/^ {3, 15} $/', $ username )){
- Return false;
- } Else {
- Return true;
- }
- }
Tip: For user name rules, you can directly use regular expression verification without using functions. this is only to demonstrate the usage of user-defined function verification.
Use method (callback) verification
ThinkPHP
Automatic Verification also supports calling a method of the current Model class for verification.
- Class UserModel extends Model {
- Protected $ _ validate = array (
- Array ('email ', 'checkemail', 'Email already exists. ', 1, 'callback '),
- };
- // CheckEmail method
- Protected function checkEmail (){
- $ User = new Model ('user ');
- // New user registration, unique verification
- If (emptyempty ($ _ POST <'uid'> )){
- If ($ user-> getByEmail ($ _ POST <'email '> )){
- Return false;
- } Else {
- Return true;
- }
- } Else {
- // Change the information to determine whether the email address is the same as the email address of another user.
- If ($ user-> where ("uid! ={$ _ POST <'uid' >}and email = '{$ _ POST <'email'>} '")-> find ()){
- Return false;
- } Else {
- Return true;
- }
- }
- }
- }
If the checkEmail method returns false
The verification fails.
It can be seen that the automatic verification function of ThinkPHP is very powerful and can meet various verification requirements for forms.