自动验证[2]
2015-05-13 17:07
28 查看
附加规则expire,设置有效期范围,必须是表单提交有效,可以是时间戳
这时,在 Home/controller/UserController.class.php 中插入
因为$data['user']在验证的时候不是验证这里有没有值,而是验证表单提交的时间,而且,如果不是表单提交,直接执行add()函数是没有用的还是会出错。
在 Home/Model/UserModel.class.php 插入以下代码:
这时验证的时间是 '2014-1-10,2015-10-10' 以内,
然后在index.html(与主目录下的index.php是同级的)插入以下代码:
注意,这里不要加上输入的时间,
附加规则callback,回调验证
在 Home/controller/UserController.class.php 插入以下代码:
然后在 Home/Model/UserModel.class.php 插入验证的代码:
附加规则function,函数验证
在 Home/Model/UserModel.class.php 内代码为:
然后在 Common 文件夹下的 Common 文件夹建立 function.php 文件,会自动加载
写入以下代码:
如果有多个字段都包含错误,默认只显示一个错误。如果想显示全部错误,可以设置属性:
这时在 Home/controller/UserController.class.php 内插入:
然后在 Home/Model/UserModel.class.php 内插入:
这时才会将两个错误都显示出来
如果想把错误信息返回给ajax处理,可以是同ajaxReturn()方法返回JSON数据。
这时在原先在 Home/controller/UserController.class.php 使用的错误显示时用的 var_dump($user->getError()); 改为 $this->ajaxReturn($user->getError());
错误信息显示如下:
还有一个就是
三. 动态验证
动态验证就是把验证的规则放在控制器端,这样,在操作的时候比较灵活,缺点就是比较混乱。
这时判定提交的数据必须加上 validate($rule) ,要不然就直接验证为'所有字段验证成功'
这时,在 Home/controller/UserController.class.php 中插入
public function add() { $user=D('User'); $data['user']=''; if($user->create($data)) { echo '所有字段验证成功'; }else { var_dump($user->getError()); } }
因为$data['user']在验证的时候不是验证这里有没有值,而是验证表单提交的时间,而且,如果不是表单提交,直接执行add()函数是没有用的还是会出错。
在 Home/Model/UserModel.class.php 插入以下代码:
<?php namespace Home\Model; use Think\Model; class UserModel extends Model { protected $_validate= array( array('user', '2014-1-10,2015-10-10', '时间已过期', 0, 'expire'), ); }
这时验证的时间是 '2014-1-10,2015-10-10' 以内,
然后在index.html(与主目录下的index.php是同级的)插入以下代码:
<meta charset="utf-8"> <form method="post" action="http://localhost/demo39/index.php/Home/User/add"> <p>用户:<input type="text" name="yonghu" /></p> <p>邮箱:<input type="text" name="youxiang" /></p> <input type="submit" value="提交"> </form>
注意,这里不要加上输入的时间,
附加规则callback,回调验证
在 Home/controller/UserController.class.php 插入以下代码:
public function add() { $user=D('User'); $data['user']='zdp'; if($user->create($data)) { echo '所有字段验证成功'; }else { var_dump($user->getError()); } }
然后在 Home/Model/UserModel.class.php 插入验证的代码:
<?php namespace Home\Model; use Think\Model; class UserModel extends Model { protected $_validate= array( array('user', 'checkLength', '用户名必须在 3-5 位', 0, 'callback', 3,array(3,5)),//v这里的array(3,5)是checkLength内的$min和$maxl两个参数 ); protected function checkLength($str,$min,$max) { preg_match_all("/./", $str, $matches); //如果验证的是中文,则正则为"/./u",后面的u是utf8编码格式 $len =count($matches[0]); if($len<$min||$len>$max) { return false; } else { return true; } } }
附加规则function,函数验证
在 Home/Model/UserModel.class.php 内代码为:
<?php namespace Home\Model; use Think\Model; class UserModel extends Model { protected $_validate= array( array('user', 'checkLength', '用户名必须在 3-5 位', 0, 'function', 3,array(3,5)),//v这里的array(3,5)是checkLength内的$min和$maxl两个参数 ); }
然后在 Common 文件夹下的 Common 文件夹建立 function.php 文件,会自动加载
写入以下代码:
<?php function checkLength($str,$min,$max) { preg_match_all("/./", $str, $matches); //如果验证的是中文,则正则为"/./u",后面的u是utf8编码格式 $len =count($matches[0]); if($len<$min||$len>$max) { return false; } else { return true; } }
如果有多个字段都包含错误,默认只显示一个错误。如果想显示全部错误,可以设置属性:
protected $patchValidate = true;
这时在 Home/controller/UserController.class.php 内插入:
public function add() { $user=D('User'); $data['user']=''; $data['email'] ='bbbb'; if($user->create($data)) { echo '所有字段验证成功'; }else { var_dump($user->getError()); } }
然后在 Home/Model/UserModel.class.php 内插入:
<?php namespace Home\Model; use Think\Model; class UserModel extends Model { protected $_validate= array( array('user', 'require', '用户名不得为空', 0, 'regex', 3), array('email', 'email', '邮箱格式不正确'), ); protected $patchValidate = true; }
这时才会将两个错误都显示出来
如果想把错误信息返回给ajax处理,可以是同ajaxReturn()方法返回JSON数据。
//返回JSON格式 $this->ajaxReturn($user->getError());
这时在原先在 Home/controller/UserController.class.php 使用的错误显示时用的 var_dump($user->getError()); 改为 $this->ajaxReturn($user->getError());
错误信息显示如下:
还有一个就是
//1指定新增数据验证,2表示修改, if ($user->create($_POST,1)) {} //一般会自动判断,就是表单提交时根据信息来判断提交来的数据是新增还是修改
三. 动态验证
动态验证就是把验证的规则放在控制器端,这样,在操作的时候比较灵活,缺点就是比较混乱。
public function add() { $rule = array( array('user','require','用户名不得为空'), ); $user=M('User'); $data['user']=''; $data['email'] ='123'; if($user->validate($rule)->create($data)) { echo '所有字段验证成功'; }else { var_dump($user->getError()); } }
这时判定提交的数据必须加上 validate($rule) ,要不然就直接验证为'所有字段验证成功'
相关文章推荐
- 运用C#在采集时进行自动验证登录
- 使用Django的ModelForm对表单进行自动验证(可自定义验证规则)
- Thinkphp框架 表单自动验证登录注册 ajax自动验证登录注册
- ThinkPHP注册自动验证及自动完成部分代码参考
- thinkphp 自动验证自动完成失效
- 01-验证文本框输入内容的长度-自动截取
- MVC 验证和异常处理 自动验证使用 IDataErrorInfo 接口
- Ext JS 登陆 Java权限验证 页面自动跳转
- SpringMVC学习系列-后记 结合SpringMVC和Hibernate-validator,根据后台验证规则自动生成前台的js验证代码
- 从零开始学习jQuery (十一) 实战表单验证与自动完成提示插件
- 详解使用Spring Security进行自动登录验证
- 使MVC3自动验证Date格式
- jQuery学习整理 (11)实战表单验证与自动完成提示插件
- 从零开始学习jQuery (十一) 实战表单验证与自动完成提示插件
- 读取短信验证码并自动设置给文本框
- Thinkphp 模型->自动验证
- ssh自动输入密码登录服务器/ssh免输入密码登录/非交互ssh 密码验证
- Android中实现短信验证码自动填入
- Thinkphp自动验证规则
- Silverlight中动态数据验证和动态自动计算的Reflection反射实现