-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.5] Support custom validation rule objects. #19155
Conversation
This is a slightly different approach to use custom validation objects compared to the current Validator::extend approach. With this addition, you can define an object that implements Illuminate\Validation\ValidationRule and then include it in your validation rules like so: ‘name’ => [new ValidName]. This provides a quick way to define custom validation rules without needing to call Validator::extend at all.
}, | ||
'name' => function ($attribute, $value, $fail) { | ||
if ($value !== 'taylor') { | ||
$fail(':attribute must be taylor'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What kind of type it is? I mean $fail
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
method
Is it possible to overwrite the validation message specified in the Rule with a custom message in the FormRequest? |
@skatika Yes. Here's what worked for me: Added code like this to the
If you're using classes instead of closures, change it to:
|
This is a slightly different approach to use custom validation objects
compared to the current Validator::extend approach. With this addition,
you can define an object that implements
Illuminate\Validation\ValidationRule and then include it in your
validation rules like so:
'name' => [new ValidName]
.This provides a quick way to define custom validation rules without
needing to call Validator::extend at all.