1

I have a form with dynamic fields (multiple users added dynamicly with one form)

I'm trying to add an error message to a specific field after a manual validation (with a basic if)

I've tried the following and none of this work

$validator->errors()->add('password.0', 'Les mots de passe ne correspondent pas');

$validator->errors()->add('password.*', 'Les mots de passe ne correspondent pas');

$validator->errors()->add('password[0]', 'Les mots de passe ne correspondent pas');

I can't make the message bag accepting my message and at the end, "$validator->errors()" doesn't contain the message for my field password[0]

Does anybody knows how to make it work?

2
  • please show us your dynamic form Commented Jun 7, 2018 at 15:08
  • <input type="text" class="form-control" name="password[0]" autofocus="" tabindex="5"> I can retrieve the value, but the issue is just with the error message bag, i can't put an error to a specific field like i showed. EDIT: I'm doing it in ajax, so the problem is that "$validator->errors()" doesn't contain my newly added error with the command indicated above Commented Jun 7, 2018 at 15:15

2 Answers 2

3

base on the inputs you have which they are arrays I think validating using laravel rules for array would be good. here is the documentation for array validations.

something like :

$validator = Validator::make($request->all(), [
    'password.*' => 'required', // your rules
] , [
'password.*.required' => "your message"
]);
Sign up to request clarification or add additional context in comments.

Comments

2

The answer of @Masoud is good, i could make it work with that specific code

'password.*' => ['required',
   function($attribute, $value, $fail) {
        $arr_explode_attr = explode(".",$attribute);
        if ($value != request()->input('password_confirmation.'.$arr_explode_attr[1])) {
                        return $fail('Les mots de passe ne correspondent pas');
    }
}];

Thanks for your help =)

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.