Validate类支持读取模型类的属性定义完成验证

This commit is contained in:
thinkphp
2016-03-07 18:02:02 +08:00
parent 440658b2e4
commit 7c1d29595b
2 changed files with 23 additions and 6 deletions

View File

@@ -1817,4 +1817,18 @@ class Model
} }
return $sql; return $sql;
} }
/**
* 获取属性值
* @access protected
* @param string $property 属性名
* @return mixed
*/
public function getProperty($property)
{
if (property_exists($this, $property)) {
return $this->$property;
}
return null;
}
} }

View File

@@ -49,7 +49,7 @@ class Validate
* 数据自动验证 * 数据自动验证
* @access protected * @access protected
* @param array $data 数据 * @param array $data 数据
* @param array $rules 验证规则 * @param mixed $rules 验证规则
* @param string $config 规则配置名 验证规则不是数组的话读取配置参数 * @param string $config 规则配置名 验证规则不是数组的话读取配置参数
* @return void * @return void
*/ */
@@ -263,7 +263,7 @@ class Validate
*/ */
public static function confirm($value, $rule, $data) public static function confirm($value, $rule, $data)
{ {
return $value == $data[$rule]; return self::getDataValue($data, $rule) == $value;
} }
/** /**
@@ -517,7 +517,10 @@ class Validate
*/ */
protected static function getDataRule($rules, $config) protected static function getDataRule($rules, $config)
{ {
if (!is_array($rules)) { if ($rules instanceof \think\Model) {
// 读取模型设置的规则
$rules = $rules->getProperty($config);
} elseif (is_string($rules)) {
// 读取配置文件中的数据类型定义 // 读取配置文件中的数据类型定义
$config = Config::get($config); $config = Config::get($config);
if (isset($config['__pattern__'])) { if (isset($config['__pattern__'])) {