Fix validation error message localization #227
Merged
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.
Added or updated the README.mdn/aRelated Issue/Intent
Ensures it is easy to customize/localize validation error messages when using the validation rules as classes or as strings.
This PR reverts #141, which reintroduced the issue in #133 where using the validation rules as strings (e.g.
enum_value:UserType
) prevented error messages from being displayed properly. #141 didn't really have any explanation of its changes, but what I think it was trying to do was allow users to easily customize the error messages for this package's validation rules. It implemented this the wrong way though, localizing the message strings themselves directly inside the language file. This has a few disadvantages:"The value you have provided is not a valid enum instance.": "Translation..."
in a JSON translation filevalidation.php
file)enum_value:UserType
Changes
This PR re-implements localization of validation error messages.
It allows this package's validation error messages to be translated by adding
enum
,enum_key
, andenum_value
keys to theen/validation.php
file that ships with Laravel, and the corresponding files for any other locales.It also allows further customizing the messages for specific cases by adding entries to the
custom
key in that file, e.g.:Notes
The reason the
trans()->has()
check in the rules is necessary is that Laravel handles validation error messages completely separately for custom rule classes and for string rules added withValidator::extend()
.For string rules, the third argument to
Validator::extend()
is treated as a fallback. If a language file entry for the snake-cased rule name exists, that will be used, otherwise the fallback will be used (source). This worked fine before #141 and still does if you manually add all the keys to your lang files, but #141 breaks it out of the box. This PR makes it work by default again, without users needing to add any language lines themselves.For class rules, the
messages()
method is the single source of truth—Laravel's validator doesn't check for custom language file entries for the key at all (source). This PR adds a check to see if any custom keys have been defined, so they'll be used instead when possible, and otherwise falls back to the ones provided by the package.