Home >Backend Development >PHP Tutorial >thinkPHP implements automatic form verification, thinkphp implements forms_PHP tutorial

thinkPHP implements automatic form verification, thinkphp implements forms_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 10:10:50914browse

thinkPHP implements automatic form verification, thinkphp implements forms

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,

Copy code The code is as follows:

public function CheckVerify($verify) {

if (md5($verify) != Session::get('verify')) return false;
        return true;
}
//Automatic verification

Copy code The code is as follows:

protected $_validate = array(
         array("title", "require", "Title is required!"),
         array('categoryId', 'require', "Category is required!"),
         array('content', 'require', "Content is required!"),
         array('verify', 'require','Verification code is required!'),
         array('verify', 'CheckVerify', 'Verification code error!', 0, 'callback')
);

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):

Copy code The code is as follows:

$validate = array(
'require'=> '/.+/',
'email' => '/^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/',
'url' => '/^http://[A-Za-z0-9]+.[A-Za-z0-9]+[/=?%-&_~`@[]':+! ]*([^<>""])*$/',
'currency' => '/^d+(.d+)?$/',
'number' => '/^d+$/',
'zip' => '/^[1-9]d{5}$/',
'integer' => '/^[-+]?d+$/',
'double' => '/^[-+]?d+(.d+)?$/',
'english' => '/^[A-Za-z]+$/',
);

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:

Copy code The code is as follows:

protected $_validate = array(
array('verify','require','Verification code is required!'), //By default, regular expression is used for verification
array(name,'','The account name already exists!',0,'unique',1), // Verify whether the name field is unique when adding it
array('value',array(1,2,3),'The value range is incorrect!',2,'in'), // Determine whether the value is within a range when it is not empty
array('repassword','password','Confirm the password is incorrect',0,'confirm'), // Verify whether the confirmation password is consistent with the password
array('password','checkPwd','Password format is incorrect',0,'function')// Custom function to verify password format
);

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! !

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/932491.htmlTechArticlethinkPHP implements automatic form verification, thinkphp implements form Last night, our boss asked me to get the automatic form verification function, and I worried about it for a long time I learned a lot of knowledge from official websites before I came out, eh, always...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn