1

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

4
  • may i know what version of laravel you are using Commented Oct 29, 2018 at 6:52
  • i am using laravel 5.7 Commented Oct 29, 2018 at 7:00
  • share your blade file Commented Oct 29, 2018 at 7:01
  • i am using vue component for this. all of my scripts are placed in vue component Commented Oct 29, 2018 at 7:14

1 Answer 1

5

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.',
        ]);
Sign up to request clarification or add additional context in comments.

5 Comments

awesome. its worked. but i don't know what was the problem on it. can you please explain more on it ?
you can understand on your own by getting the validation error on dd(); example: $validatedData = $request->validate([ your validation rules ]); and then dd($validatedData); thanks
it seems you have not made any changes in question's code you just copy it and paste it as it is so how can you solved the above problem, please explain
@ShaielndraGupta Kamrul has already explained see You should add child dimension errors before parent fields
check the last line and second last line. 2nd dimensional validation (sections.*) should be placed before 1st dimension validation error. If not then the validation message replaced with "The department name field is required."

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.