ThinkPHP 表单自动验证运用示例_PHP

WBOY
Release: 2016-05-31 19:29:33
Original
752 people have browsed it

ThinkPHP

使用TP 3.2框架

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

验证规则也可以写到模型里,但我感觉有些麻烦,一是有时候不同页面验证的方式会不一样,二是看到这个add_post事件里的代码,就清楚要接收什么数据,如何验证数据能够在第一眼有个大致了解,所以总结出了此方式。

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!