Controller和Model类增加failException方法 用于设置验证失败后是否抛出异常

This commit is contained in:
thinkphp
2016-06-04 17:52:47 +08:00
parent 326ef84f3b
commit d041ee665c
2 changed files with 40 additions and 2 deletions

View File

@@ -14,6 +14,7 @@ namespace think;
use think\Cache;
use think\Db;
use think\db\Query;
use think\Exception;
use think\Loader;
use think\model\Relation;
use think\paginator\Collection as PaginatorCollection;
@@ -85,6 +86,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
protected $relation;
// 属性类型
protected $fieldType = [];
// 验证失败是否抛出异常
protected $failException = false;
/**
* 初始化过的模型.
@@ -703,6 +706,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return $this;
}
/**
* 设置验证失败后是否抛出异常
* @access public
* @param bool $fail 是否抛出异常
* @return $this
*/
public function failException($fail = true)
{
$this->failException = $fail;
return $this;
}
/**
* 自动验证数据
* @access protected
@@ -729,7 +744,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
}
if (!$validate->check($data)) {
$this->error = $validate->getError();
return false;
if ($this->failException) {
throw new Exception($this->error);
} else {
return false;
}
}
$this->validate = null;
}