Skip to content
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

Fix regex pattern matching #1122

Merged
merged 6 commits into from
Aug 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function run(array $data = null, string $group = null): bool

if (is_string($rules))
{
$rules = explode('|', $rules);
$rules = $this->splitRules($rules);
}

$value = dot_array_search($rField, $data);
Expand Down Expand Up @@ -693,6 +693,51 @@ protected function getErrorMessage(string $rule, string $field, string $label =
return $message;
}

/**
*
* Split rules string by pipe operator.
*
* @param string $rules
*
* @return array
*/
protected function splitRules(string $rules): array
{
$_rules = [];
$pipe_pos = strpos($rules, '|');
while ($pipe_pos !== false) {

// the if is for the regex_match
// if the pattern contains | (pipe) the split was incorrect
// so we make sure that, if the pipe is in a pattern we find the separator and
// grab the string up to the separator + closing bracket
$open_bracket_pos = strpos($rules, '[');
if ($open_bracket_pos !== false && $open_bracket_pos < $pipe_pos) {
$separator = $rules[$open_bracket_pos + 1];

$regex_end_pos = strpos($rules, $separator . ']');
$_rules[] = substr($rules, 0, $regex_end_pos + 2);

$rules = substr($rules, $regex_end_pos + 3);

$pipe_pos = strpos($rules, '|');
continue;
}

$_rules[] = substr($rules, 0, $pipe_pos);
$rules = substr($rules, $pipe_pos + 1);

$pipe_pos = strpos($rules, '|');
}

// if there is another rule remaining but no separator just add it to the list
if (!empty($rules)) {
$_rules[] = $rules;
}

return array_unique($_rules);
}

//--------------------------------------------------------------------
//--------------------------------------------------------------------
// Misc
Expand Down
4 changes: 4 additions & 0 deletions tests/system/Validation/FormatRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ public function testRegexMatch()
{
$data = [
'foo' => 'abcde',
'phone' => '0987654321',
];

$this->validation->setRules([
'foo' => 'regex_match[/[a-z]/]',
'phone' => 'regex_match[/^(01[2689]|09)[0-9]{8}$/]',
]);

$this->assertTrue($this->validation->run($data));
Expand All @@ -59,10 +61,12 @@ public function testRegexMatchFalse()
{
$data = [
'foo' => 'abcde',
'phone' => '09876543214',
];

$this->validation->setRules([
'foo' => 'regex_match[\d]',
'phone' => 'regex_match[/^(01[2689]|09)[0-9]{8}$/]',
]);

$this->assertFalse($this->validation->run($data));
Expand Down
32 changes: 32 additions & 0 deletions tests/system/Validation/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,4 +501,36 @@ public function testHasError()
$this->assertTrue($this->validation->hasError('foo'));
}

//--------------------------------------------------------------------


public function testSplitRulesTrue()
{
$data = [
'phone' => '0987654321',
];

$this->validation->setRules([
'phone' => 'required|regex_match[/^(01[2689]|09)[0-9]{8}$/]|numeric',
]);

$result = $this->validation->run($data);

$this->assertTrue($result);
}

public function testSplitRulesFalse()
{
$data = [
'phone' => '09876543214',
];

$this->validation->setRules([
'phone' => 'required|regex_match[/^(01[2689]|09)[0-9]{8}$/]|numeric',
]);

$result = $this->validation->run($data);

$this->assertFalse($result);
}
}