您的位置:首页 > 其它

自动验证[2]

2015-05-13 17:07 28 查看
附加规则expire,设置有效期范围,必须是表单提交有效,可以是时间戳

这时,在 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) ,要不然就直接验证为'所有字段验证成功'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: