-
Notifications
You must be signed in to change notification settings - Fork 11.3k
[12.x] Introduce Rule::anyOf()
for Validating Against Multiple Rule Sets (#54880)
#54946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thanks for submitting a PR! Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface. Pull requests that are abandoned in draft may be closed due to inactivity. |
Rule::anyOf()
for Validating Against Multiple Rule Sets (#54880)
The fact that this simple version of the code doesn't work is confusing to me. I don't know why it should be limited to array fields. $validator = new Validator(
resolve('translator'),
[
'email' => 'test@example.com'
],
[
'email' => Rule::anyOf([
['required', 'min:20'],
['required', 'email'],
])
],
);
$this->assertTrue($validator->passes()); Added a failing test for this. |
Conceptually nothing should impede that; I had simply opted to expect the exception that the validator would throw in case it found something it didn't like:
The obvious and easiest solution is just wrapping the $validator = new Validator(
resolve('translator'),
[
'email' => '20charstringtestvalidation'
],
[
'email' => Rule::anyOf([
'required|min:20',
'required|email',
])
],
);
$this->assertTrue($validator->passes()); example changediff --git a/src/Illuminate/Validation/Rules/AnyOf.php b/src/Illuminate/Validation/Rules/AnyOf.php
index 7086a30c4f..04a20854cb 100644
--- a/src/Illuminate/Validation/Rules/AnyOf.php
+++ b/src/Illuminate/Validation/Rules/AnyOf.php
@@ -4,6 +4,7 @@
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\ValidatorAwareRule;
+use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Validator;
use InvalidArgumentException;
@@ -59,8 +60,8 @@ public function passes($attribute, $value)
foreach ($this->ruleSets as $ruleSet) {
$validator = Validator::make(
- $value,
- $ruleSet,
+ Arr::wrap($value),
+ Arr::wrap($ruleSet),
$this->validator->customMessages,
$this->validator->customAttributes
); Thanks for the suggestion! I already have changes locally that are ready to be pushed upon confirmation 😄 |
Yes, I would probably expect that to work as well. Thanks @brianferri |
Failure seems to be unrelated to this PR |
@brianferri just added another simple test that should pass but fails. |
* feat: add more validation tests * wip: add failing test * wip: add basic string rule validations * chore: rename object fields for better debugging * refactor: rename ruleSets to rules * fix: respect array rule validation --------- Co-authored-by: Christian Ascone <ascone.christian@gmail.com> * fix: this should be passing because AnyOf has no type relevance and 'required' only checks to see if the field has something in it --------- Co-authored-by: Christian Ascone <ascone.christian@gmail.com> --------- Co-authored-by: Christian Ascone <ascone.christian@gmail.com>
please refer to #55191