laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.32k stars 10.95k forks source link

Laravel's validation overrides custom message specified in InvokableRule #44902

Closed abbluiz closed 1 year ago

abbluiz commented 1 year ago

Description:

Even though I'm passing this as the message in my custom validation rule:

// $attribute = 'cpf'
if ($repeatedDocuments->isNotEmpty()) {
    $fail('validation.user_document_must_be_unique')->translate([
        'attribute' => trans("validation.attributes.${attribute}"),
    ]);
}

Which translates to The document CPF already exists in our database. (I checked in xdebug, it actually returns the correct message).

However, after the $fail() callback finishes executing, Laravel's validator somehow overrides this with another rule message, which is validation.cpf. Given that "cpf" is the value of $attribute in this case, it's using validation.${attribute} as the message key even though I specified something else via $fail() callback.

Steps To Reproduce:

  1. Use a custom rule that returns any message, while having validation.${attribute} passed as custom message keys (or stored in your translation files);
  2. Watches Laravel return the message of validation.${attribute} instead of the one you passed in $fail()

This problem DID NOT occur in Laravel 9.19.0

driesvints commented 1 year ago

Heya, thanks for reporting.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here? Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Please do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

driesvints commented 1 year ago

Closing until a repo is provided.