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] Standardise invokable rule translation functionality #42873

Merged
merged 2 commits into from
Jun 20, 2022

Conversation

timacdonald
Copy link
Member

@timacdonald timacdonald commented Jun 20, 2022

This PR aligns the translation capabilities of invokable validation rules with the rest of the translation system. The initial PR had some opinions baked in that on reflection I don't believe should have been present and should instead be left to the developer to handle.

This PR:

  • removes the strict nature of the invokable / callable translation. On reflection the initial PR introduced a quirk to the translation stuff that made it act differently to the rest of the system. Although I personally liked the strict nature of throwing when the key doesn't exist - it is a departure from what already exists and I think consistency is best. If this functionality is wanted, it should be implemented at the translator level.
  • Adds the ability to specify replacements for messages. The standard :attribute, :index, and :position replacements are already in place, so this just adds custom replacement key capabilities:
// validation.php

return [
    'is' => 'The :attribute is not :value.',
];

// Rule class...

class IsRule implements InvokableRule
{
    public function __construct(private mixed $value)
    {
        //
    }

    public function __invoke($attribute, $value, $fail)
    {
        if ($this->value !== $value) {
            $fail('validation.translated-error')->translate([
                'value' => $this->value,
            ]);
        }
    }
}

// Form request rules...

return [
    'email' => new IsRule('tim@example.com'),
];

// error...

"The email is not tim@example.com."
  • enables developers to specify on-the-fly locales for error messages...
$fail('validation.translated-error')->translate([
    'value' => $this->value,
], 'fr'); // Specifying french translation.
  • and also enables translation with choices for pluralisation support.

With these features in place, this should cover all the require translation support for the new invokable / closure based rule stuff.

@timacdonald timacdonald force-pushed the validation-translation branch from 726dd74 to d18845d Compare June 20, 2022 00:15
@timacdonald timacdonald changed the title [9.x] Improve invokable rule translation capabilities [9.x] Standardise invokable rule translation functionality Jun 20, 2022
@timacdonald timacdonald force-pushed the validation-translation branch from ae490b4 to 6e19e61 Compare June 20, 2022 12:11
@timacdonald timacdonald force-pushed the validation-translation branch from 6e19e61 to 242b33c Compare June 20, 2022 12:13
@taylorotwell taylorotwell merged commit a271c24 into laravel:9.x Jun 20, 2022
@timacdonald timacdonald deleted the validation-translation branch June 24, 2022 05:37
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