【问题标题】:Modify Laravel Validation message response修改 Laravel 验证消息响应
【发布时间】:2015-07-12 11:32:56
【问题描述】:

通常验证消息与我的 json 响应类似

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

但我想做

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

【问题讨论】:

  • 如果有帮助,您可以在下面接受我的回答以供更多读者阅读:)

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


【解决方案1】:

你可以使用array_flatten()助手:

array_flatten($validator->getMessageBag()->getMessages());

这将返回所有验证器错误的一维数组。

阅读更多关于 Laravel 助手的信息:http://laravel.com/docs/5.0/helpers

【讨论】:

  • 它将只返回数组中没有属性名称的消息。像 [ "这些字段是必需的。"]
【解决方案2】:

用途:

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

"email.required": "email 字段为必填项。" } 它适用于 laravel 4

【讨论】:

    【解决方案3】:

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

    创建了一个扩展 Illuminate\Support\MessageBag 并覆盖 add 方法的 CustomMessageBag

    <?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;
        }
    
    }
    

    然后创建了一个使用CustomMessageBag 类的customValidator

    <?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;
        }
    }
    

    最后在AppServiceProvider'sboot方法中注册CustomValidator

    <?php
    
    namespace App\Providers;
    
    use Illuminate\Support\ServiceProvider;
    use App\Extend\CustomValidator;
    use Event;
    use Mail;
    use Config;
    use Lang;
    use Log;
    
    class AppServiceProvider extends ServiceProvider {
    
        /**
         * Bootstrap any application services.
         *
         * @return void
         */
        public function boot() {
            //Register custom validator class
            $this->app->validator->resolver(function($translator, $data, $rules, $messages) {
                return new CustomValidator($translator, $data, $rules, $messages);
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-10
      • 2023-03-28
      • 1970-01-01
      • 2018-12-17
      • 2016-02-23
      • 1970-01-01
      • 1970-01-01
      • 2018-01-31
      相关资源
      最近更新 更多