mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-06 23:02:48 +08:00
改进Model类的数据验证机制
This commit is contained in:
@@ -299,6 +299,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
public function save($data = [], $where = [], $getId = true)
|
public function save($data = [], $where = [], $getId = true)
|
||||||
{
|
{
|
||||||
if (!empty($data)) {
|
if (!empty($data)) {
|
||||||
|
// 数据自动验证
|
||||||
|
if (!$this->validateData($data)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
// 数据对象赋值
|
// 数据对象赋值
|
||||||
foreach ($data as $key => $value) {
|
foreach ($data as $key => $value) {
|
||||||
$this->__set($key, $value);
|
$this->__set($key, $value);
|
||||||
@@ -307,10 +311,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
$this->isUpdate = true;
|
$this->isUpdate = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 数据自动验证
|
|
||||||
if (!$this->validateData()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检测字段
|
// 检测字段
|
||||||
if (!empty($this->field)) {
|
if (!empty($this->field)) {
|
||||||
@@ -517,9 +517,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
/**
|
/**
|
||||||
* 自动验证当前数据对象值
|
* 自动验证当前数据对象值
|
||||||
* @access public
|
* @access public
|
||||||
|
* @param array $data 验证数据
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validateData()
|
public function validateData($data)
|
||||||
{
|
{
|
||||||
if (!empty($this->validate)) {
|
if (!empty($this->validate)) {
|
||||||
$info = $this->validate;
|
$info = $this->validate;
|
||||||
@@ -537,7 +538,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
$validate->scene($scene);
|
$validate->scene($scene);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!$validate->check($this->data)) {
|
if (!$validate->check($data)) {
|
||||||
$this->error = $validate->getError();
|
$this->error = $validate->getError();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user