Home > Backend Development > PHP Tutorial > thinkPHP form automatic validation function

thinkPHP form automatic validation function

WBOY
Release: 2016-08-08 09:32:17
Original
1127 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 <span>function</span><span> CheckVerify($verify) {

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

//Automatic verification

   protected $_validate =<span> array(
        array(</span>"title", "require", "标题必须!"<span>),
        array(</span>'categoryId', 'require', "类别必须!"<span>),
        array(</span>'content', 'require', "内容必须!"<span>),
        array(</span>'verify', 'require','验证码必须!'<span>),
        array(</span>'verify', 'CheckVerify', '验证码错误!', 0, 'callback'<span>)
    );</span>
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 =<span> array(
 </span>2 
 3             'require'=> '/.+/'<span>,
 </span>4 
 5             'email' => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/'<span>,
 </span>6 
 7             'url' => '/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/'<span>,
 </span>8 
 9             'currency' => '/^\d+(\.\d+)?$/'<span>,
</span>10 
11             'number' => '/^\d+$/'<span>,
</span>12 
13             'zip' => '/^[1-9]\d{5}$/'<span>,
</span>14 
15             'integer' => '/^[-\+]?\d+$/'<span>,
</span>16 
17             'double' => '/^[-\+]?\d+(\.\d+)?$/'<span>,
</span>18 
19             'english' => '/^[A-Za-z]+$/'<span>,
</span>20 
21         );<br>
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  =<span>  array(   
array(</span>'verify','require','验证码必须!'), <span>//</span><span>默认情况下用正则进行验证   </span>
array(name,'','帐号名称已经存在!',0,’unique’,1), <span>//</span><span> 在新增的时候验证name字段是否唯一   </span>
array('value',array(1,2,3),'值的范围不正确!',2,’<span>in</span>’), <span>//</span><span> 当值不为空的时候判断是否在一个范围内   </span>
array('repassword','password','确认密码不正确',0,’confirm’), <span>//</span><span> 验证确认密码是否和密码一致   </span>
array('password','checkPwd','密码格式不正确',0,’<span>function</span>’)<span>//</span><span> 自定义函数验证密码格式  </span>
);
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