[8.x] Add ValidatorAwareRule interface #37442
Merged
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.
In #36960 Nuno added a
DataAwareRule
interface which allows aRule
to have access to the data under validation. This is so great !Unfortunately, it does not allows the
Rule
to gain access to :Why do we need this ? I will give it some examples but possibilities are limitless
1 - If we want to check some model existence with some custom condition
Actually
or
Here the
bail
is required to ensure thatAuthorExists::passes
is not executed with unsafe data.After
The bail here in included directly in the rule, which matches what is done with
exists
rule (see https://github.com/laravel/framework/blob/8.x/src/Illuminate/Validation/Validator.php#L657 and https://github.com/laravel/framework/blob/8.x/src/Illuminate/Validation/Validator.php#L733)2 - We may want the rule applied in different ways depending on the previous defined rules.
Which is the way that
size
,min
, ... actually work.Another use case would be to fetch the
date_format
in order to have create aDateTime
and make assertions about this :3 - 4 - Imagine we are building a booking service and we want to validate that the location is available a given day.
With this PR we could simply do something like this :
TL;DR : this PR adds only a few lines in the framework but I believe opens up great possibilities and power to custom
Rule
s