[11.x] Improve Email validation rule custom translation messages
#54202
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Via #54067 we introduced the fluent
Emailvalidation rule. While implementing this new feature in our project I noticed that the existing custom validation message was not being used.This PR adds tests that test custom validation messages, and makes changes to the
Emailclass to make the tests pass. This change alsoThis PR makes the following diff possible:
public function rules(): array { return [ 'answer.email_address' => [ 'required', 'string', - new class extends Email { - public function message(): string - { - return __('check_answer.validation.email.invalid'); - } - }, + Email::default(), ], ]; } public function messages(): array { return [ 'answer.email_address.required' => __('check_answer.validation.email.required'), 'answer.email_address.string' => __('check_answer.validation.email.invalid'), 'answer.email_address.email' => __('check_answer.validation.email.invalid'), ]; }The
Emailclass is quite some slimmer due to now converting to the old rules and then validating that, instead of implementing the different email validation classes. This way the email specific validation and thecustomRulescan just be merged, similar to how theFilerule works.The
ValidationEmailRuleTest(andValidationPasswordRuleTestandValidationFileRuleTest) are quite confusing, the helper methods are nice for simple tests, but complex tests/cases result in cognitive overload. I would be happy to see if we can simplify these tests, and also test more cases, e.g. the other existing rules don't test custom validation messages.The implementation of the
messages()method in the differentRuleclasses inIlluminate\Validation\Rulesalso differ to a confusing extend. Let me know if you are open to receiving follow-up PRs to cleanup the framework in this area.