Authored by ziy

优化数据校验

... ... @@ -17,7 +17,11 @@ class Language
"alpha_dash" => "{attribute} 只能由字母、数字和斜杠组成.",
"alpha_num" => "{attribute} 只能由字母和数字组成.",
"array" => "{attribute} 必须是一个数组.",
"between" => "{attribute} 必须介于 {min} - {max} (个字符\个单元)之间.",
"between" => [
"numeric" => "{attribute} 必须介于 {min} - {max} 之间.",
"string" => "{attribute} 必须介于 {min} - {max} 个字符之间.",
"array" => "{attribute} 必须只有 {min} - {max} 个单元.",
],
"boolean" => "{attribute} 必须为布尔值.",
"confirmed" => "{attribute} 两次输入不一致.",
"date" => "{attribute} 不是一个有效的日期.",
... ... @@ -29,13 +33,26 @@ class Language
"integer" => " {attribute} 必须是整数.",
'string' => '{attribute} 必须是字符.',
"ip" => "{attribute} 必须是有效的 IP 地址.",
"max" => "{attribute} 不能大于 {max} (个字符\个单元).",
"min" => "{attribute} 至少有 {min} (个字符\个单元).",
"max" => [
"numeric" => "{attribute} 不能大于 {max}.",
"string" => "{attribute} 不能大于 {max} 个字符.",
"array" => "{attribute} 最多只有 {max} 个单元.",
],
"min" => [
"numeric" => "{attribute} 必须大于等于 {min}.",
"string" => "{attribute} 至少为 {min} 个字符.",
"array" => "{attribute} 至少有 {min} 个单元.",
],
"numeric" => "{attribute} 必须是一个数字.",
"regex" => "{attribute} 格式不正确.",
"required" => "{attribute} 不能为空.",
"same" => "{attribute}{other} 必须相同.",
"size" => "{attribute} 必须是 {size} (大小\个字符\个单元).",
"size" => [
"numeric" => "{attribute} 大小必须为 {size}.",
"string" => "{attribute} 必须是 {size} 个字符.",
"array" => "{attribute} 必须为 {size} 个单元.",
],
"active_url" => "{attribute} 不是一个有效的网址.",
"url" => "{attribute} 格式不正确。",
];
}
\ No newline at end of file
... ...
... ... @@ -13,36 +13,69 @@ class Messages
{
private $messages = array();
private $messagesAttribute = array();
private $customMessages = array();
private $defaultMessagesLanguage = array();
public function __construct(array $messagesAttribute = array())
public function __construct(array $customMessages = array())
{
$this->defaultMessagesLanguage = Language::$messages;
$this->messagesAttribute = $messagesAttribute;
$this->customMessages = $customMessages;
}
public function messagesAttribute(array $messagesAttribute = array())
public function customMessages(array $customMessages = array())
{
$this->messagesAttribute = $messagesAttribute;
$this->customMessages = $customMessages;
}
/**
* 获取所有错误
* @return array
*/
public function all()
{
return $this->messages;
$all = array();
foreach ($this->messages as $key => $messages) {
$all = array_merge($all, $messages);
}
return $all;
}
public function setMessages($key, $val)
{
$this->messages[$key] = $val;
$this->messages[$key][] = $val;
}
/**
* 查看所有字段的所有错误信息
* @param $key
* @return array
*/
public function get($key)
{
if (isset($this->messages[$key])) {
return $this->messages[$key];
}
return array();
}
/**
* 判断是否有错误信息
* @return bool
*/
public function has()
{
return isset($this->messages[$key]);
}
public function getMessages($key)
public function getMessages($key, $type = null)
{
if (isset($this->messagesAttribute[$key])) {
return $this->messagesAttribute[$key];
if (isset($this->customMessages[$key])) {
return $this->customMessages[$key];
} elseif (array_key_exists($key, $this->defaultMessagesLanguage)) {
if (isset($this->defaultMessagesLanguage[$key][$type])) {
return $this->defaultMessagesLanguage[$key][$type];
}
return $this->defaultMessagesLanguage[$key];
}
return '';
... ...
... ... @@ -11,6 +11,18 @@
);
字段值通过 PHP 函数 checkdnsrr 来验证是否为一个有效的网址。
```
##url
```php
Validator::make(
['url'=>'active_url']
)
Validator::make(
['url_str' => 'http://www.baidu.com'],
['url_str' => 'url']
);
网址格式不正确。
```
##after
```php
Validator::make(
... ... @@ -304,4 +316,22 @@
['date_time' => 'size:1']
);
字段值的尺寸需符合给定 value 值。对于字串来说,value 为需符合的字串长度。对于数字来说,value 为需符合的整数值
```
##code
```php
判断是否有错误
if ($validator->fails() == true) {
}
判断是否通过
if ($validator->passes() == true) {
}
获取所有错误
foreach ($validator->messages()->all() as $key => $val) {
}
获取指定错误
$validator->messages()->get('email');
```
\ No newline at end of file
... ...
... ... @@ -16,16 +16,14 @@ class Validator
private $data;
private $rules;
private $failedRules;
protected $failedRules = array();
protected $sizeRules = array('Size', 'Between', 'Min', 'Max');
private $rules;
protected $numericRules = array('Numeric', 'Integer');
protected $implicitRules = array(
'Required', 'RequiredWith', 'RequiredWithAll', 'RequiredWithout', 'RequiredWithoutAll', 'RequiredIf', 'Accepted'
'Required'
);
protected $messagesRules = array(
... ... @@ -33,7 +31,7 @@ class Validator
'alphanum' => 'alpha_num',
'digitsbetween' => 'digits_between',
'notin' => 'not_in',
'activeurl'=>'active_url'
'activeurl' => 'active_url'
);
/**
... ... @@ -41,16 +39,21 @@ class Validator
*/
private $messages;
public function __construct(array $data, array $rules, array $messagesAttribute = array())
public function __construct(array $data, array $rules, array $customMessages = array())
{
$this->messages = new Messages($messagesAttribute);
$this->messages = new Messages($customMessages);
$this->data = $this->parseData($data);
$this->rules = $this->explodeRules($rules);
}
public function messagesAttribute(array $messagesAttribute = array())
/**
* 自定义错误信息
* @param array $customMessages
* @return $this
*/
public function customMessages(array $customMessages = array())
{
$this->messages->messagesAttribute($messagesAttribute);
$this->messages->customMessages($customMessages);
return $this;
}
... ... @@ -83,6 +86,10 @@ class Validator
return $rules;
}
/**
* 通过
* @return bool
*/
public function passes()
{
foreach ($this->rules as $attribute => $rules) {
... ... @@ -109,19 +116,32 @@ class Validator
$validatable = $this->isValidatable($rule, $attribute, $value);
$method = "validate{$rule}";
if ($validatable && !$this->$method($attribute, $value, $parameters, $this)) {
$this->addError($attribute, $rule, $parameters);
$this->addError($attribute, $rule, $parameters, $value);
$this->failedRules[$attribute][$rule] = $parameters;
}
}
protected function addError($attribute, $rule, $parameters)
protected function addError($attribute, $rule, $parameters, $value)
{
$messagesKey = strtolower($rule);
if (isset($this->messagesRules[$messagesKey])) {
$messagesKey = $this->messagesRules[$messagesKey];
}
$message = $this->messages->getMessages($messagesKey);
$message = $this->messages->getMessages($messagesKey, $this->getValueType($value));
$message = $this->doReplacements($message, $attribute, $rule, $parameters);
$this->messages->setMessages(strtolower($attribute . '.' . $rule), $message);
$this->messages->setMessages(strtolower($attribute), $message);
}
protected function getValueType($value)
{
if (is_numeric($value)) {
return 'numeric';
} elseif (is_array($value)) {
return 'array';
} elseif (is_string($value)) {
return 'string';
}
return null;
}
protected function getAttributeType($attribute)
... ... @@ -299,6 +319,16 @@ class Validator
return checkdnsrr($url);
}
protected function validateUrl($attribute, $value)
{
return filter_var($value, FILTER_VALIDATE_URL) !== false;
}
protected function replaceUrl($message, $attribute, $rule, $parameters)
{
return str_replace('{attribute}', $attribute, $message);
}
protected function validateAfter($attribute, $value)
{
return strtotime($value) ? true : false;
... ... @@ -536,6 +566,6 @@ class Validator
public function messages()
{
return $this->messages->all();
return $this->messages;
}
}
\ No newline at end of file
... ...