修改Laravel验证消息响应

时间:2015-05-02 20:28:43

标签: php validation laravel laravel-4 laravel-validation

通常验证消息与我的json响应一样

"error_details": {
    "password": "The password field is required.",
"email": "The email field is required."
}

但我想制作

DocumentRoot

2 个答案:

答案 0 :(得分:0)

使用:

"error_details": {
"password.required": "The password field is required.",

" email.required":"电子邮件字段是必需的。" } 它适用于laravel 4

答案 1 :(得分:0)

我偶然发现了同样的问题。我是这样做的

创建了"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '), DATEDIFF(COALESCE(e.time_period_to, NOW()), e.time_period_from), 0) / COUNT(D' at line 1" 类,扩展了CustomMessageBag并覆盖了Illuminate\Support\MessageBag方法

add

然后创建了一个使用<?php namespace App\Extend; use Config; use Illuminate\Support\MessageBag as BaseMessageBag; /** * Extending Validation Class * */ class CustomMessageBag extends BaseMessageBag{ /** * Add a message to the bag. * * @param string $key * @param string $message * @return $this */ public function add($key, $message) { if ($this->isUnique($key, $message)) { //remove additional array $this->messages[$key] = $message; } return $this; } }

customValidator
CustomMessageBag

最后在<?php namespace App\Extend; use Input; use Lang; use Config; use App\Extend\CustomMessageBag as MessageBag; /** * Extending Validation Class * */ class CustomValidator extends \Illuminate\Validation\Validator { /** * Determine if the data passes the validation rules. * * @return bool */ public function passes() { $this->messages = new MessageBag; // We'll spin through each rule, validating the attributes attached to that // rule. Any error messages will be added to the containers with each of // the other error messages, returning true if we don't have messages. foreach ($this->rules as $attribute => $rules) { foreach ($rules as $rule) { $this->validate($attribute, $rule); } } // Here we will spin through all of the "after" hooks on this validator and // fire them off. This gives the callbacks a chance to perform all kinds // of other validation that needs to get wrapped up in this operation. foreach ($this->after as $after) { call_user_func($after); } return count($this->messages->all()) === 0; } } CustomValidator方法

中注册AppServiceProvider's课程
boot