Grathand72
Grathand72

Reputation: 53

Modify Laravel Validation message response

generally validation message look like with my json response

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

but I want to make

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

Upvotes: 2

Views: 1652

Answers (3)

Arun Unnikrishnan
Arun Unnikrishnan

Reputation: 2349

I did stumble upon this same problem. Here is how I did it

Created a CustomMessageBag class which extends Illuminate\Support\MessageBag and overrides the add method

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

}

Then created a customValidator which uses the CustomMessageBag class

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

Finally register the CustomValidator class in AppServiceProvider's boot method

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

Upvotes: 0

Jlcanizales
Jlcanizales

Reputation: 21

Use:

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

"email.required": "The email field is required." } It works on laravel 4

Upvotes: 0

Limon Monte
Limon Monte

Reputation: 54409

You can use array_flatten() helper:

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

This will return the one-dimensional array of all validator errors.

Read more about Laravel helpers: http://laravel.com/docs/5.0/helpers

Upvotes: 2

Related Questions