Emtiaz Zahid
Emtiaz Zahid

Reputation: 2835

Wrong message showing in Laravel validation for multidimensional array

I have this validation rules and i appended some custom messages for those validations.

 $this->validate($request, [
            'name'=>'required',
            'departments.*.name'=>'required',
            'departments.*.sections.*.name'=>'required',
        ],[
            'name.required'=>'The division name field is required.',
            'departments.*.name.required'=>'The department name field is required.',
            'departments.*.sections.*.name.required'=>'The section name field is required.',
        ]);

Screenshot of my view:

enter image description here

Here you can see, the empty section input field showing error message

"The department name field is required."

But it should be show

"The section name field is required."

What am i missing?

Note: i am printing the first index of all fields error messages

Upvotes: 1

Views: 1167

Answers (1)

Kamrul Touhid
Kamrul Touhid

Reputation: 156

Its replacing the error messages from departments.*.sections.*.name.required

You should add child dimension errors before parent fields

try to use this:

 $this->validate($request, [
            'name'=>'required',
            'departments.*.name'=>'required',
            'departments.*.sections.*.name'=>'required',
        ],[
            'name.required'=>'The division name field is required.',
            'departments.*.sections.*.name.required'=>'The section name field is required.',
            'departments.*.name.required'=>'The department name field is required.',
        ]);

Upvotes: 5

Related Questions