You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Laravel supports adding custom replacers, which are called when making replacements in validation error messages. This functionality, while still available, is no longer documented since version 8.x. It looks like extending the validator has been generally superseded by custom rule objects and closures.
However, some complex validation rules may still benefit from providing custom replacements. For example, consider a simple Contains rule which checks if an array contains all the required values. Ideally, the validation error would only list the missing values, ie:
Here, a custom validation error message is provided when creating the validator, and the message uses the :missing placeholder. While it is still possible register a replacer using Validator::replacer(), it's not optimal, since the replacer has no access to the failed rule instance, so it does not know which values were missing. Also, it splits the concerns of a single rule across different files, as Rule itself should not register custom replacers with the Validator (otherwise, a new replacer would be registered for each new rule).
As a solution, it would be great if the Rule class could provide a makeReplacements instance method, which could make the replacements for the message, while having access to all instance data. For example:
This would require some changes in the FormatsMessages trait, as it does not pass the rule instance to its own makeReplacements method (which is public). I imagine something like this:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Laravel supports adding custom replacers, which are called when making replacements in validation error messages. This functionality, while still available, is no longer documented since version 8.x. It looks like extending the validator has been generally superseded by custom rule objects and closures.
However, some complex validation rules may still benefit from providing custom replacements. For example, consider a simple
Contains
rule which checks if an array contains all the required values. Ideally, the validation error would only list the missing values, ie:Here, a custom validation error message is provided when creating the validator, and the message uses the
:missing
placeholder. While it is still possible register a replacer usingValidator::replacer()
, it's not optimal, since the replacer has no access to the failed rule instance, so it does not know which values were missing. Also, it splits the concerns of a single rule across different files, as Rule itself should not register custom replacers with the Validator (otherwise, a new replacer would be registered for each new rule).As a solution, it would be great if the
Rule
class could provide amakeReplacements
instance method, which could make the replacements for the message, while having access to all instance data. For example:This would require some changes in the
FormatsMessages
trait, as it does not pass the rule instance to its ownmakeReplacements
method (which is public). I imagine something like this:Beta Was this translation helpful? Give feedback.
All reactions