Skip to content

Commit

Permalink
Add "Not Regex" validation rule (#23475)
Browse files Browse the repository at this point in the history
  • Loading branch information
vlakoff authored and taylorotwell committed Mar 10, 2018
1 parent c917557 commit 55b4efe
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
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

0 comments on commit 55b4efe

Please sign in to comment.