ThinkPHP form automatic verification example, thinkphp example. ThinkPHP form automatic verification example, thinkphp example uses TP3.2 framework publicfunctionadd_post () {verification rule $ rulearray (array (name, require, please enter name, 1 ), the ThinkPHP form must be used for automatic verification.
Use the TP 3.2 Framework
Public function add_post () {// verification rule $ rule = array ('name', 'require ', 'Enter name', 1 ), // name must be verified); $ m = M ('user'); // Obtain the name, sex, and contact data to the model, and verify if (! $ M-> field ('name, sex, contact ')-> validate ($ rule)-> create ()) $ this-> error ($ m-> getError (); $ result = $ m-> add (); if (! $ Result) $ this-> error ('add failed'); $ this-> success ('added successfully', U ('dir '));}
Verification rules can also be written to the model, but I feel a little troublesome. first, sometimes different page verification methods are used, and second, the code in this add_post event is displayed, it is clear what data to receive and how to verify that the data can be roughly understood at the first glance, so this method is summarized.
Isn't form verification of thinkphp ajax? Is there a better form verification tool than thinkphp? For example, jquery form
ThinkPHP's background framework does not have ajax itself. It is easy to submit with jquery form. You can take a look at the official thinkphp example.
Www.thinkphp1.cn/extend/230.html
Automatic Verification of thinkphp
Let me show you an example:
// Form verification
Protected $ _ validate = array (
// Array ('verification field', 'verification rule', 'error message', verification criteria, additional rules, verification time)
Array ('uname', 'require ', 'user name must be verified! ', 1, 'regex', 3 ),
// Array ('username', '', 'username already exists ', 1, 'Unique', 1 ),
Array ('pwd', 'require ', 'password is required! '),
Array ('pwd', 'checkpwd', 'password length not less than 6 bits, 1, 'callback '),
);
Function checkPwd (){
$ Password = $ _ POST ['pwd'];
If (strlen ($ password)> = 6 ){
Return true;
} Else {
Return false;
}
}
// Form ing
Protected $ _ map = array (
'Uname' => 'username ',
'Pwd' => 'password ',
);
// Automatic completion
Protected $ _ auto = array (
// Array (fill field, fill content, fill condition, add rule) fill condition: 1, insert 2, Update 3, all
Array ('Reg _ date', 'getdate', 1, 'callback '),
Array ('password', 'md5', 3, 'Function '),
);
Function getDate (){
Return date ('Y-m-d H: I: s ');
}
Example of automatic http://www.bkjia.com/PHPjc/893423.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/893423.htmlTechArticleThinkPHP form verification application, thinkphp example using TP 3.2 Framework public function add_post () {// validation rules $ rule = array ('name', 'require ', 'enter the name', 1), // must be verified...