thinkphp Form Auto-validation example, thinkphp example
Using the TP 3.2 framework
Public Function Add_post () {///Validation Rule $rule=array (Array (' name ', ' Require ', ' Please enter name ', 1),//must verify name), $m =m (' user ');//Get Name , sex,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 (' Add Success ', U (' dir '));}
Validation rules can also be written into the model, but I feel a little trouble, one is sometimes different page verification method will not be the same, the second is to see the code in this Add_post event, it is clear what data to receive, how to verify that the data can have a general understanding at the first glance, so summed up this way.
thinkphp form validation is not Ajax? Is there a better form validation tool than thinkphp? Like jquery form.
Thinkphp's background framework, which itself does not have Ajax. Using jquery form to submit is good, very convenient. You can take a look at the official thinkphp example
Www.thinkphp.cn/extend/230.html
thinkphp Auto-validation issues
show you an example I wrote:
//form validation
Protected $_validate=array (
//array (' Validation field ', ' Validation rule ', ' Error prompt ', validation criteria, additional rules , authentication time)
Array (' uname ', ' require ', ' username must be verified! ', 1, ' Regex ', 3),
//array (' username ', ', ' username already exists ', 1, ' unique ', 1),
Array (' pwd ', ' require ', ' password must be filled in! '),
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 Map
Protected $_map=array (
' uname ' = ' username ',
' pwd ' = ' password ',
);
AutoComplete
Protected $_auto=array (
//array (fill field, fill content, fill condition, additional 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 ');
}
http://www.bkjia.com/PHPjc/893423.html www.bkjia.com true http://www.bkjia.com/PHPjc/893423.html techarticle thinkphp form Automatic validation using the example, the thinkphp example uses the TP 3.2 Framework public Function Add_post () {//Validation rule $rule=array (Array (' name ', ' Require ', ' Please enter the name ', 1),//Must be inspected ...