Open striker4150 opened 9 months ago
Yeah this is a difficult one, and probably one for v5 it basically requires a rewrite of the whole rule inferring system.
The RequiredRuleInferrer
always adds a required rule because the type is not nullable.
Then the AttributesRuleInferrer
adds the required attributes, it then will remove the required rule we've added earlier for RequiredUnless
, then when adding RequiredWith
we again remove a required rule if present. Sadly enough this is RequiredUnless
.
I've quickly tried a special method when adding the attributes, to not replace the requires but that also fails in a lot of tests.
We need a new RuleInferrer for this, basically one that completely replaces the existing ones we have and merges them into one rule inferrer so that we can be smarter about inferring rules. At the moment we don't have an idea what's happening in the other rule inferrers.
Technically, this shouldn't be a breaking change since we could just add that rule inferrer, I'll take a look in the coming weeks if I can find the time to implement such a thing. PR's are also always welcome.
I've already added a test which tests this case: https://github.com/spatie/laravel-data/blob/e954107a25eebe903228f34bf961f148114520d5/tests/ValidationTest.php#L279-L290
Dear contributor,
because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.
Dear contributor,
because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.
✏️ Describe the bug Adding multiple requiring rules to a property/parameter only results in the last added requiring rule being applied. For example, if a
RequiredUnless
rule is used alongside aRequiredWith
rule, only theRequiredWith
rule will be applied (assuming that theRequiredWith
rule is last).The offending lines seem to be: https://github.com/spatie/laravel-data/blob/324970e910414b92b43a239843efd06fa51090e9/src/Support/Validation/PropertyRules.php#L40-L44
↪️ To Reproduce
✅ Expected behavior In the scenario above, I expect the
a
property to have the requiring rules['required_unless:requires_a,false', 'required_with:b']
. However, thea
property only has therequired_with:b
. Laravel does offer the ability to apply multiple requiring rules at once, as demonstrated here:I expect requiring rules to be applied even if they are of the same type. The only requiring rule that I would expect to not be duplicated is the plain
Required
rule.🖥️ Versions
Laravel: 10.x Laravel Data: 3.11.0 PHP: 8.1.15