Skip to content

Commit 1e08dc6

Browse files
authored
Merge pull request #1220 from ytetsuro/fix/split-validation-rules
Improved division logic of validation rules.
2 parents 309245e + a4f6343 commit 1e08dc6

2 files changed

Lines changed: 14 additions & 39 deletions

File tree

system/Validation/Validation.php

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -707,43 +707,18 @@ protected function getErrorMessage(string $rule, string $field, string $label =
707707
*/
708708
protected function splitRules(string $rules): array
709709
{
710-
$_rules = [];
711-
$pipe_pos = strpos($rules, '|');
712-
while ($pipe_pos !== false)
713-
{
714-
// the if is for the regex_match
715-
// if the pattern contains | (pipe) the split was incorrect
716-
// so we make sure that, if the pipe is in a pattern we find the separator and
717-
// grab the string up to the separator + closing bracket
718-
$open_bracket_pos = strpos($rules, '[');
719-
if ($open_bracket_pos !== false && $open_bracket_pos < $pipe_pos)
720-
{
721-
$separator = $rules[$open_bracket_pos+1];
722-
723-
if (preg_match('/(?<!\\\\)(?:\\\\\\\\)*\\'.$separator.'\]/', $rules, $matches, PREG_OFFSET_CAPTURE))
724-
{
725-
$regex_end_pos = $matches[0][1];
726-
727-
$_rules[] = substr($rules, 0, $regex_end_pos+2);
728-
729-
$rules = substr($rules, $regex_end_pos+3);
730-
731-
$pipe_pos = strpos($rules, '|');
732-
continue;
733-
}
734-
}
735-
736-
$_rules[] = substr($rules, 0, $pipe_pos);
737-
$rules = substr($rules, $pipe_pos+1);
738-
739-
$pipe_pos = strpos($rules, '|');
740-
}
741-
742-
// if there is another rule remaining but no separator just add it to the list
743-
if (! empty($rules))
744-
{
745-
$_rules[] = $rules;
746-
}
710+
$non_escape_bracket = '((?<!\\\\)(?:\\\\\\\\)*[\[\]])';
711+
$pipe_not_in_bracket = sprintf(
712+
'/\|(?=(?:[^\[\]]*%s[^\[\]]*%s)*(?![^\[\]]*%s))/',
713+
$non_escape_bracket,
714+
$non_escape_bracket,
715+
$non_escape_bracket
716+
);
717+
718+
$_rules = preg_split(
719+
$pipe_not_in_bracket,
720+
$rules
721+
);
747722

748723
return array_unique($_rules);
749724
}

tests/system/Validation/ValidationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,8 @@ public function testSplitRegex()
550550
{
551551
$method = $this->getPrivateMethodInvoker($this->validation, 'splitRules');
552552

553-
$result = $method('required|regex_match[/^[0-9]{4}[\-\.\/][0-9]{2}[\-\.\/][0-9]{2}/]|max_length[10]');
553+
$result = $method('required|regex_match[/^[0-9]{4}[\-\.\[\/][0-9]{2}[\-\.\[\/][0-9]{2}/]|max_length[10]');
554554

555-
$this->assertEquals('regex_match[/^[0-9]{4}[\-\.\/][0-9]{2}[\-\.\/][0-9]{2}/]', $result[1]);
555+
$this->assertEquals('regex_match[/^[0-9]{4}[\-\.\[\/][0-9]{2}[\-\.\[\/][0-9]{2}/]', $result[1]);
556556
}
557557
}

0 commit comments

Comments
 (0)