...
|
...
|
@@ -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 |
...
|
...
|
|