Format validation error message as nested associative array

I have a Json object of data as shown below

{
  "name": "something",
  "location": {
    "city": "some where",
    "country": "some where",

  }
}

Rule used to validate Request is

[
    'name' => 'required',
    'location.city' => 'required',
    'location.country' => 'required'
]

Which returns error message like

{
  "name": [
    "The name field is required."
  ],
  "location.city": [
    "The location.city field is required."
  ],
  "location.county": [
    "The location.country field is required."
  ]
}

How can I format error message as a nested array like the Request.

{
  "name": [
    "The name field is required."
  ],
  "location": {
    "city": [
      "The city field is required"
    ],
    "country": [
      "The country field is required"
    ]
  }
}

Any default methods available ? I am using Illuminate\Foundation\Http\FormRequest

3 answers

  • answered 2018-11-08 08:21 Mou Hsiao

    Is it? Laravel Documentation- Customizing the Error Messages

    public function messages()
    {
        return [
            'location.city' => 'The city field is required',
            'location.county'  => 'The county field is required',
        ];
    }
    

  • answered 2018-11-08 08:37 N69S

    In your case , you need to build the error message yourself. you can still use the default messages in the ressources/lang/en/validation messages file.

    $validator = Validator::make($request->all(), [
        'name' => 'required',
        'location.city' => 'required',
        'location.country' => 'required'
    ]);
    
    if ($validator->fails()) {
        return response()->json($yourOwnFormat,422);
        //you can use $validator->errors() to build it
    }
    

  • answered 2018-11-08 09:52 Ebin Manuval

    For those who looking for the solution This is how I implemented

    <?php 
    namespace App\Http\Requests;
    
    use Illuminate\Contracts\Validation\Validator;
    use Illuminate\Foundation\Http\FormRequest;
    use Illuminate\Http\Exceptions\HttpResponseException;
    
    class UserStoreRequest extends FormRequest
    {
        public function rules()
        {
            return [
                'name' => 'required',
                'location.city' => 'required'
                'location.country' => 'required'
            ];
        }
        public function attributes()
        {
            return [
                'location.city' => 'City'
                'location.country' => 'Country'
            ];
        }
        protected function failedValidation(Validator $validator)
        {
            $errors = $validator->errors()->getMessages();
            $errors_formated = array();
            foreach ($errors as $key => $value) {
            array_set($errors_formated, $key, $value);
            }
            throw new HttpResponseException(response()->json(['error' => $errors_formated], 422));
        }
    }
    

    The result of $validator->errors()->getMessages() is just like array_dot() helper function result. So I did the Opposite of array_dot(), also altered my attribute name into pretty name