thinkPHP form automatic validation function

WBOY
Release: 2016-08-08 09:32:17
Original
1001 people have browsed it

Last night our boss asked me to create the automatic form 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 can see protected $_validate = array(); // Automatically verify that it is of array type. Define it in the corresponding data model file below;

2,

public function CheckVerify($verify) {

   if (md5($verify) != Session::get('verify')) return false;
        return true;
}
Copy after login

//Automatic verification

   protected $_validate = array(
        array("title", "require", "标题必须!"),
        array('categoryId', 'require', "类别必须!"),
        array('content', 'require', "内容必须!"),
        array('verify', 'require','验证码必须!'),
        array('verify', 'CheckVerify', '验证码错误!', 0, 'callback')
    );
Copy after login


3. Format description: array (verification fields, verification rules, error prompts, verification conditions, additional rules, verification 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):

 1 $validate = array(
 2 
 3             'require'=> '/.+/',
 4 
 5             'email' => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',
 6 
 7             'url' => '/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/',
 8 
 9             'currency' => '/^\d+(\.\d+)?$/',
10 
11             'number' => '/^\d+$/',
12 
13             'zip' => '/^[1-9]\d{5}$/',
14 
15             'integer' => '/^[-\+]?\d+$/',
16 
17             'double' => '/^[-\+]?\d+(\.\d+)?$/',
18 
19             'english' => '/^[A-Za-z]+$/',
20 
21         );
Copy after login

Prompt information: Definition of prompt information after verification failure (required),
Verification conditions: (optional)
There are three rules for verification 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 Verify when the value is not empty
Additional rules:
regex Regular verification, indicating that the previous verification rule is a regular expression;
function Use function verification, indicating that the previous That 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, and 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, and the previously defined one 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 whether the same value exists. Value;
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, these verification rules can be used directly;

Verification time: (optional)

01.Model:: MODEL_INSERT or 1 to verify when adding data
02.Model:: MODEL_UPDATE or 2 to verify when editing data
03.Model:: MODEL_BOTH or 3 to verify in all cases (default)

5. Official example:

protected $_validate  =  array(   
array('verify','require','验证码必须!'), //默认情况下用正则进行验证   
array(name,'','帐号名称已经存在!',0,’unique’,1), // 在新增的时候验证name字段是否唯一   
array('value',array(1,2,3),'值的范围不正确!',2,’in’), // 当值不为空的时候判断是否在一个范围内   
array('repassword','password','确认密码不正确',0,’confirm’), // 验证确认密码是否和密码一致   
array('password','checkPwd','密码格式不正确',0,’function’)// 自定义函数验证密码格式  
);
Copy after login

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

The above introduces the thinkPHP form automatic verification function, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!