改进Model类的数据验证机制

This commit is contained in:
thinkphp
2016-06-03 16:07:07 +08:00
parent 99acf3d0b0
commit 57e72fcee0

View File

@@ -299,6 +299,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
public function save($data = [], $where = [], $getId = true)
{
if (!empty($data)) {
// 数据自动验证
if (!$this->validateData($data)) {
return false;
}
// 数据对象赋值
foreach ($data as $key => $value) {
$this->__set($key, $value);
@@ -307,10 +311,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$this->isUpdate = true;
}
}
// 数据自动验证
if (!$this->validateData()) {
return false;
}
// 检测字段
if (!empty($this->field)) {
@@ -517,9 +517,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
/**
* 自动验证当前数据对象值
* @access public
* @param array $data 验证数据
* @return bool
*/
public function validateData()
public function validateData($data)
{
if (!empty($this->validate)) {
$info = $this->validate;
@@ -537,7 +538,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$validate->scene($scene);
}
}
if (!$validate->check($this->data)) {
if (!$validate->check($data)) {
$this->error = $validate->getError();
return false;
}