Authored by ziy

优化提示错误信息

... ... @@ -33,7 +33,18 @@ class Messages
*/
public function customMessages(array $customMessages = array())
{
$this->_customMessages = $customMessages;
$_customMessages = array();
foreach ($customMessages as $key => $custom) {
if (is_string($custom)) {
$_customMessages[$key] = $custom;
} elseif (is_array($custom)) {
foreach ($custom as $_key => $_val) {
$_customMessages[$key . '.' . $_key] = $_val;
}
}
}
unset($customMessages);
$this->_customMessages = $_customMessages;
}
/**
... ... @@ -117,15 +128,17 @@ class Messages
* @param null $type
* @return string
*/
public function getMessages($key, $type = null)
public function getMessages($attribute, $rule, $type = null)
{
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];
if (isset($this->_customMessages[$attribute . '.' . $rule])) {
return $this->_customMessages[$attribute . '.' . $rule];
} elseif (isset($this->_customMessages[$attribute])) {
return $this->_customMessages[$attribute];
} elseif (array_key_exists($rule, $this->defaultMessagesLanguage)) {
if (isset($this->defaultMessagesLanguage[$rule][$type])) {
return $this->defaultMessagesLanguage[$rule][$type];
}
return $this->defaultMessagesLanguage[$key];
return $this->defaultMessagesLanguage[$rule];
}
return '';
}
... ...
... ... @@ -123,13 +123,13 @@ class Validator
protected function addError($attribute, $rule, $parameters, $value)
{
$messagesKey = strtolower($rule);
if (isset($this->messagesRules[$messagesKey])) {
$messagesKey = $this->messagesRules[$messagesKey];
$_rule = strtolower($rule);
if (isset($this->messagesRules[$_rule])) {
$_rule = $this->messagesRules[$_rule];
}
$message = $this->messages->getMessages($messagesKey, $this->getValueType($value));
$message = $this->messages->getMessages($attribute, $_rule, $this->getValueType($value));
$message = $this->doReplacements($message, $attribute, $rule, $parameters);
$this->messages->setMessages(strtolower($attribute), $message);
$this->messages->setMessages((strtolower($attribute) . '.' . strtolower($rule)), $message);
}
protected function getValueType($value)
... ...