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

[5.3] Fix validator messages for implicit attributes with errors #15538

Merged
merged 1 commit into from
Sep 21, 2016
Merged

[5.3] Fix validator messages for implicit attributes with errors #15538

merged 1 commit into from
Sep 21, 2016

Conversation

themsaid
Copy link
Member

In the validations language file you can set a custom attribute name for implicit attributes:

'attributes' => [
    'image.*' => 'Images'
],

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:

'attributes' => [
    'image.1' => 'Images'
],

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:

'attributes' => [
    'image' => ['1' => 'Images']
],

Internally trans() uses Arr: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 calling Arr::get($attributes, 'image.1') will check for exact key image.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 use Translator::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.

@taylorotwell taylorotwell merged commit b7b049a into laravel:5.3 Sep 21, 2016
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.

2 participants