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

[5.6] Add "Not Regex" validation rule #23475

Merged
merged 1 commit into from
Mar 10, 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
19 changes: 19 additions & 0 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,25 @@ public function validateRegex($attribute, $value, $parameters)
return preg_match($parameters[0], $value) > 0;
}

/**
* Validate that an attribute does not pass a regular expression check.
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return bool
*/
public function validateNotRegex($attribute, $value, $parameters)
{
if (! is_string($value) && ! is_numeric($value)) {
return false;
}

$this->requireParameterCount(1, $parameters, 'not_regex');

return preg_match($parameters[0], $value) < 1;
}

/**
* Validate that a required attribute exists.
*
Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Validation/ValidationRuleParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,9 @@ protected static function parseStringRule($rules)
*/
protected static function parseParameters($rule, $parameter)
{
if (strtolower($rule) == 'regex') {
$rule = strtolower($rule);

if ($rule === 'regex' || $rule === 'notregex') {
return [$parameter];
}

Expand Down
21 changes: 18 additions & 3 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2330,19 +2330,34 @@ public function testValidateTimezone()
public function testValidateRegex()
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['x' => 'asdasdf'], ['x' => 'Regex:/^([a-z])+$/i']);
$v = new Validator($trans, ['x' => 'asdasdf'], ['x' => 'Regex:/^[a-z]+$/i']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => 'aasd234fsd1'], ['x' => 'Regex:/^([a-z])+$/i']);
$v = new Validator($trans, ['x' => 'aasd234fsd1'], ['x' => 'Regex:/^[a-z]+$/i']);
$this->assertFalse($v->passes());

// Ensure commas are not interpreted as parameter separators
$v = new Validator($trans, ['x' => 'a,b'], ['x' => 'Regex:/^a,b$/i']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => '12'], ['x' => 'Regex:/^12$/i']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => 123], ['x' => 'Regex:/^123$/i']);
$v = new Validator($trans, ['x' => 12], ['x' => 'Regex:/^12$/i']);
$this->assertTrue($v->passes());
}

public function testValidateNotRegex()
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['x' => 'foo bar'], ['x' => 'NotRegex:/[xyz]/i']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => 'foo xxx bar'], ['x' => 'NotRegex:/[xyz]/i']);
$this->assertFalse($v->passes());

// Ensure commas are not interpreted as parameter separators
$v = new Validator($trans, ['x' => 'foo bar'], ['x' => 'NotRegex:/x{3,}/i']);
$this->assertTrue($v->passes());
}

Expand Down