search
Homephp教程php手册ThinkPHP 表单自动验证运用示例,thinkphp示例

ThinkPHP 表单自动验证运用示例,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'));
}

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

thinkphp的表单验证不是ajax的?有比thinkphp的表单验证工具更好用的?比如jquery form

ThinkPHP的后台框架,本身并没有ajax。用jquery form提交就好,很方便。你可以看一下thinkphp官方的示例
www.thinkphp.cn/extend/230.html
 

thinkphp自动验证的问题

给你看我写的一个例子:
//表单验证
protected $_validate=array(
//array('验证字段','验证规则','错误提示',验证条件,附加规则,验证时间)
array('uname','require','用户名必须验证!',1,'regex',3),
//array('username','','用户名已经存在',1,'unique',1),
array('pwd','require','密码必须填写!'),

array('pwd','checkPwd','密码长度不少于6位',1,'callback'),
);

function checkPwd(){
$password=$_POST['pwd'];
if(strlen($password)>=6){
return true;
}else {
return false;
}
}
//表单映射
protected $_map=array(
'uname'=>'username',
'pwd'=>'password',
);
//自动完成功能
protected $_auto=array(
//array(填充字段,填充内容,填充条件,附加规则) 填充条件:1,插入 2,更新 3,所有的
array('reg_date','getDate',1,'callback'),
array('password','md5',3,'function'),
);
function getDate(){
return date('Y-m-d H:i:s');
}
 

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.