lighthouse icon indicating copy to clipboard operation
lighthouse copied to clipboard

Validation attribute translations for nested arguments

Open eNzyOfficial opened this issue 3 years ago • 3 comments

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

eNzyOfficial avatar Nov 09 '22 05:11 eNzyOfficial

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.

spawnia avatar Nov 09 '22 19:11 spawnia

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.

eNzyOfficial avatar Nov 10 '22 02:11 eNzyOfficial

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.

spawnia avatar Nov 10 '22 08:11 spawnia