[5.3] Fix validator messages for implicit attributes with errors #15538
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.
In the validations language file you can set a custom attribute name for implicit attributes:
This would replace the attribute name for all image errors with
Images
and will produce messages like "Images is required" instead of "image.0 is required".You can also have this:
Which will replace only
image.1
with "Images" keeping the rest of the messages to be displayed as is.The problem is that the validator uses
$this->trans('validations.attributes.image.1')
which expects a format like this:Internally
trans()
usesArr:get()
which searches for the key using the dot format.However,
Arr:get()
checks for the exact key first before assuming it's in dot format so callingArr::get($attributes, 'image.1')
will check for exact keyimage.1
before checking for['image'][1]
, for this Instead of calling$this->trans('validations.attributes.image.1')
I collect all attributes translations using$this->trans->get('validation.attributes')
and then use array get to get the keys.That's the main purpose of the PR, however in the Validator test we use Symfony's translator instead of Laravel's translator because it contains an
AddResource
method we use in tests, but passing Symfony's translator while we useTranslator::get()
method, which only exists in Laravel's, throws an exception.So in this PR I added an
addLines()
method to Laravel's translator and converted the tests to use Laravel's translator and this method instead of Symfony's.