[8.x] Allow validation error attribute names to fallback to a more generic translation #36587
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Use case
There is a complex form with similar fields present in different groups. For instance, within a network form, we want to define the name and IP of machines having some specific roles. The same field could in some cases have a specific translation but in most cases it would always be translated the same way.
Current
The validation attributes translation definition would look like that:
Notice that if the user add some more roles to the form, he will then have to define more translations: e.g.
'network.*.dns.host_name' => 'Server name'. This becomes even more cumbersome as the number of fields grow per group.Notice also that in the use case described in laravel/ideas#2533 (dynamic validation rules per form item) we simply cannot get any translation.
Proposed fix
When a specific translation is missing, we could simply look if we find a more generic translation key. In the above example, we could simply the above translations to:
When the
network.*.db_slave.host_nameis looked up, we will sequentially look for the following keys within thevalidation.attributesarray:network.*.db_slave.host_name*.db_slave.host_namedb_slave.host_namehost_nameGain from the user perspective
This allows the user to drastically lower the number of translations used, and to lower maintainance cost when new similar fields are added.
In the application we are working on, we currently have around 500 keys of attribute translations, with a lot of duplicate entries. The proposed fixe would allow to divide that number by around 3, leaving around 150 unique keys.
The gain is obvious when it comes to changing a translation (having to make sure we do not forget a key) and adding new groups of fields (not forgetting to define the specific duplicate for that particular group).
Further improvements
I think the same enhancement could be provided for the
validation.valuestranslations too.fixes laravel/ideas#2533 and laravel/ideas#1771