ThinkPHP form automatic verification application example, thinkphp example_PHP tutorial

WBOY
Release: 2016-07-13 10:17:19
Original
989 people have browsed it

ThinkPHP form automatic verification application example, thinkphp example

Using TP 3.2 framework

public function add_post(){
//验证规则
$rule=array(
array('name','require','请输入姓名',1),//必须验证name
);

$m=M('user');

//获取name,sex,contact数据到模型,并验证
if(!$m->field('name,sex,contact')->validate($rule)->create())
$this->error($m->getError());

$result=$m->add();

if(!$result)
$this->error('添加失败');

$this->success('添加成功',U('dir'));
}
Copy after login

Validation rules can also be written into the model, but I find it a bit troublesome. First, sometimes different pages have different verification methods. Second, when you see the code in the add_post event, you will know what data to receive and how. The verification data can give you a general understanding at first glance, so this method is summarized.

Thinkphp’s form validation is not ajax? Is there a better form validation tool than thinkphp? For example, jquery form

ThinkPHP’s backend framework does not have ajax itself. Just use jquery form to submit, it is very convenient. You can take a look at thinkphp’s official example
www.thinkphp.cn/extend/230.html

Thinkphp automatic verification problem

Let me show you an example I wrote:
//Form validation
protected $_validate=array(
//array('validation field','validation rule','error prompt', Verification conditions, additional rules, verification time)
array('uname','require','Username must be verified!',1,'regex',3),
//array('username', '','The username already exists',1,'unique',1),
array('pwd','require','The password must be filled in!'),

array('pwd ','checkPwd','Password length is not less than 6 characters',1,'callback'),
);

function checkPwd(){
$password=$_POST['pwd '];
if(strlen($password)>=6){
return true;
}else {
return false;
}
}
// Form mapping
protected $_map=array(
'uname'=>'username',
'pwd'=>'password',
);
//Autocomplete function
protected $_auto=array(
//array(fill field, fill content, fill condition, additional rules) 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');
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/893423.htmlTechArticleThinkPHP form automatic verification application example, thinkphp example uses TP 3.2 framework public function add_post(){//Validation rules $ rule=array(array('name','require','Please enter your name',1),//must be verified...
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!