-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[9.x] Regex validation rules for array items containing 'or' seperators cause ErrorException on input validation #40924
Comments
You should wrap your $v = new Validator($trans, ['x' => 'foo'], ['x' => 'Regex:/^(taylor|james)$/i']);
$this->assertTrue($v->passes()); But this will pass: -$v = new Validator($trans, ['x' => 'foo'], ['x' => 'Regex:/^(taylor|james)$/i']);
+$v = new Validator($trans, ['x' => 'foo'], ['x' => ['Regex:/^(taylor|james)$/i']]);
$this->assertTrue($v->passes()); |
Note that this is what the documentation instructs you to do, when your regex contains a |
Sorry, I see this is more nuanced as it's affecting nested rules only. I'll re-open whilst I investigate. |
The regex rule is actually in array form; I could understand that using something like |
Right, this passes in 8.x but fails in 9.x. $v = new Validator($trans,
['x' => ['y' => ['z' => 'james']]],
['x.*.z' => [
'required',
'string',
'Regex:/^(taylor|james)$/i'
]]
);
$this->assertTrue($v->passes());
|
@stevebauman any idea how to solve this? Otherwise, we need to revert Rule::forEach |
Give me a moment -- investigating now 👍 |
@stevebauman from my investigation so far the error is around here: framework/src/Illuminate/Validation/ValidationRuleParser.php Lines 145 to 161 in 4c7cd8c
If I replace the lines above by their 8.x counterpart... framework/src/Illuminate/Validation/ValidationRuleParser.php Lines 133 to 137 in 29bc877
...it works (tested I am still digging into it, but I thought it would be nice to share |
@stevebauman in 8.x when framework/src/Illuminate/Validation/ValidationRuleParser.php Lines 85 to 98 in 4c7cd8c
It was wrapped into an array, now it reaches here as a string, thus it gets on the first One other thing: if the regex validation rule is a top-level rule it works. It only fails when it is nested. |
@stevebauman if we change this line:
back to as it was in 8.x foreach ((array) $rules as $rule) { It works. From the blame this line was changed by you on PR #40498 I will run the tests with this change and report back. EDIT: it works with the test case provided by OP |
@stevebauman Changing it back as of my last comment makes these two tests fail:
I have an appointment in 10 minutes, so I will only be able to look into it further later. Hope it helps you out :) |
Description:
Using an or sign (
|
) within a regex validation rule for an array item breaks on Laravel 9.x. This works fine on Laravel 8.x.It seems somewhere the rules are parsed and the
|
is interpreted as start of another validation rule instead of as being part of the regex rule, causing the parsed regex rule to beregex:/^(typeA|
.Most probably related to #40498.
Steps To Reproduce:
See https://github.com/jnoordsij/laravel/tree/regex-validation-bug-8.x for the (working) 8.x version and https://github.com/jnoordsij/laravel/tree/regex-validation-bug-9.x for the test breaking on 9.x.
composer install
cp .env.example .env.testing
php artisan key:generate --env=testing
php artisan test tests/Feature/SimpleRequestTest.php
Resulting exception:
ErrorException: preg_match(): No ending delimiter '/' found in ...
The text was updated successfully, but these errors were encountered: