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] Fix array to string conversion when using custom validation messages for size rules #40074

Merged
merged 4 commits into from
Dec 16, 2021
Merged

[9.x] Fix array to string conversion when using custom validation messages for size rules #40074

merged 4 commits into from
Dec 16, 2021

Conversation

mateusjunges
Copy link
Contributor

Description

This PR is a follow up on #36885. I've tried ealier this year, but i think i haven't described correctly the issues or used wrong examples and it was rejected, so i'll try it again. When using custom validation messages for size rules, if you define your custom validation messages in `resources/lang/en/validation.php, then laravel thrown an exception when accessing those messages:

image

This exception comes from the following line, while replacing the :message:

return str_replace([':message', ':key'], [$message, $messageKey], $format);

I'm sending this to master because if some user currently uses the custom validation messages, then the response he gets when using $validator->errors() is the following:

{
    "file":[
        {
           "file":"The file must be between 20 and 50 kilobytes.",
           "string":"Some custom message here"
        }
    ]
}

and this PR changes it to:

{
    "file":[
        "Some custom message here"
    ]
}

So, this changes the way you access validation errors from $validator->errors()->toArray()['file'][0]['string'] to $validator->errors()->toArray()['file'][0] or $validator->errors()->first('file), which is a breaking change.

Steps to reproduce

  • Define custom validation messages in resources/lang/en/validation.php:
'custom' => [
    'file' => [
        'between' => [
            'numeric' => 'The :attribute must be between :min and :max.',
            'file' => 'The :attribute must be between :min and :max kilobytes.',
            'string' => 'Some custom message here',
            'array' => 'The :attribute must have between :min and :max items.',
        ],
    ],
],
  • Create a test route in web.php to test the validation message:
Route::get('/test', function() {
   return Validator::make(['file' => 'filename.png'], ['file' => 'between:20,50'])->errors()->first('file');
});
  • Go to localhost:8000/test

@mateusjunges mateusjunges changed the title fix array to string conversion when using custom validation messages … [9.x] fix array to string conversion when using custom validation messages … Dec 16, 2021
@mateusjunges mateusjunges changed the title [9.x] fix array to string conversion when using custom validation messages … [9.x] Fix array to string conversion when using custom validation messages for size rules Dec 16, 2021
@mateusjunges
Copy link
Contributor Author

Here #36885 (comment) taylor said that "an email is never a file or a numeric. It's basically string". I agree and i really should have described it better. But, with the file attribute, you could have two or more places in your project that uses file sometimes as string (filename) and sometimes as a file (uploaded file). So, if you use custom validation messages, then you get the array to string conversion. That's why i'm submitting this PR again.

@taylorotwell taylorotwell merged commit a1f66ba into laravel:master Dec 16, 2021
@mateusjunges mateusjunges deleted the array-to-string-conversion-on-custom-validation-messages branch December 16, 2021 16:28
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

2 participants