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

Validation attribute translations for nested arguments #2227

Open
eNzyOfficial opened this issue Nov 9, 2022 · 3 comments
Open

Validation attribute translations for nested arguments #2227

eNzyOfficial opened this issue Nov 9, 2022 · 3 comments
Labels
enhancement A feature or improvement

Comments

@eNzyOfficial
Copy link
Contributor

What problem does this feature proposal attempt to solve?

I'm struggling with translations, the problem is when returning the validation errors, if the args were nested it spits out something like input.name and it doesnt resolve __('validation.attributes.name') like it would with a normal laravel validator due to the input. prefix

The required translation from the validation.php file in lang directory:
'required' => 'ข้อมูล:attribute จำเป็นต้องกรอก',

Paired with 'attributes' => ['name' => 'TRANSLATION HERE'] in the same file

Lighthouse will spit out:
'input.name' => 'ข้อมูลinput.name จำเป็นต้องกรอก'

Because of the input. prefix, it doesn’t resolve and translate the attribute in the message

Is there a way to get around this without having to duplicate all of the attributes and prefix them with input. (what if it’s nested further, like a relation, you have to duplicate them all further with the same value but a different key

https://laravel.com/docs/9.x/validation#specifying-attribute-in-language-files

Which possible solutions should be considered?

If it’s not currently possible, my proposed solution would be to keep the key the same input.name in the returned error list, but pass through the name from the rules for the message name instead of input.name

This would be a breaking change, so maybe we could make it a toggled feature in the config or a directive or something? Open to suggestions

@spawnia spawnia added the enhancement A feature or improvement label Nov 9, 2022
@spawnia spawnia changed the title Validation attribute translations Validation attribute translations for nested arguments Nov 9, 2022
@spawnia
Copy link
Collaborator

spawnia commented Nov 9, 2022

I am not planning to work on this myself, but can provide guidance for a pull request. All validation concerns are handled in src/Validation. In order to implement this, look into RulesGatherer and how it is used in ValidateDirective.

@eNzyOfficial
Copy link
Contributor Author

Sure, I can try to make a PR soon. Should I make this a toggled feature from the config? That's the only way I can think of implementing this without it being a breaking change.

@spawnia
Copy link
Collaborator

spawnia commented Nov 10, 2022

Yeah, a feature toggle would be great if there is breakage. Allows for a smoother transition to a new major version where we might change the default or remove the toggle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A feature or improvement
Projects
None yet
Development

No branches or pull requests

2 participants