Reputation: 57
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?
Upvotes: 1
Views: 1330
Reputation: 57
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 =)
Upvotes: 2
Reputation: 883
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"
]);
Upvotes: 3