Skip to content
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

[9.x] Allow specifiying custom messages for Rule objects #41145

Merged
merged 1 commit into from
Feb 21, 2022
Merged

[9.x] Allow specifiying custom messages for Rule objects #41145

merged 1 commit into from
Feb 21, 2022

Conversation

ryangjchandler
Copy link
Contributor

@ryangjchandler ryangjchandler commented Feb 21, 2022

There's not currently any simple way to specify a custom message when validating with a rule object.

Here are the current userland work-arounds and caveats:
Ensure custom rule uses translation strings for message
This would affect all uses of the message. Not very useful for changing the message in a single place.

Extending the rule object with a custom message() method
Not a very nice API. A lot of work for a simple thing.

Interacting with the rule object from a Closure rule and using $fail
Also not very nice. Requires knowledge of the Rule's dependencies, etc (think 3rd party packages).

With the changes in this PR, you can now provide a custom message to the $messages array.

// A rule that would fail...
class Example implements Rule
{

}

$request->validate(rules: [
	'foo' => [new Example]
], messages: [
	Example::class => 'My custom message goes here!'
])

In this scenario, the custom messages provided to $messages would be used when the rule fails instead of the default message.

NOTE: I'm not sure if the simple test I've provided is enough. Should I add tests to cover custom messages that have translation replacements in them too?

@ryangjchandler
Copy link
Contributor Author

ryangjchandler commented Feb 21, 2022

I'm going to add some more tests to this:

  • Key-specific messages, i.e. 'foo.' . Example::Class
  • Translation replacements

@ryangjchandler ryangjchandler marked this pull request as draft February 21, 2022 16:56
@ryangjchandler ryangjchandler marked this pull request as ready for review February 21, 2022 17:13
@dennisprudlo
Copy link
Contributor

Love it! Had that recently with the new Enum rule which I used to replace most of the Rule::in() rules. After realizing that I cannot simply modify the messages I switched back to the in-rule.

@taylorotwell taylorotwell merged commit e705ccd into laravel:9.x Feb 21, 2022
@taylorotwell
Copy link
Member

Thanks!

@taylorotwell
Copy link
Member

@ryangjchandler can you make a PR to the docs?

@ryangjchandler
Copy link
Contributor Author

@taylorotwell Yeah, I can do that tomorrow if that's okay?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants