2

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."
}
1
  • 1
    you can accept my answer below for further readers if it was helpful :) Commented May 18, 2015 at 9:25

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

It will return only messages without the attribute name in the array. like [ "The fields is required."]
0

Use:

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

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.