Home > Article > Backend Development > thinkPHP form automatic verification function, thinkphp form verification_PHP tutorial
Last night our boss asked me to work on the form automatic verification function, and I spent a long time worrying about it and borrowed a lot of knowledge from official websites before I came up with it, eh , 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>
//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>
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):
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 /><br />
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 field exists Just verify (default)
Model::MUST_TO_VALIDATE or 1 Must verify
Model::VALUE_TO_VAILIDATE or 2 Verify when the value is not empty
Additional rules:
regex regular verification, indicating the previous verification The 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 that in the form Whether two fields are equal, the verification rule is a field name;
equal verifies whether it is equal to a certain value, which is defined by the previous verification rule;
in verifies whether it is within a certain range, defined previously is an array;
unique is used to verify 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, 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> );
I don’t know if it meets the requirements of our boss, but it’s still coming out. It’s so hard! ! I spent the whole night looking for codes, cases, and comfort! !