diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index 28e2b7b6e2a7..e528ebc22d43 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -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. * diff --git a/src/Illuminate/Validation/ValidationRuleParser.php b/src/Illuminate/Validation/ValidationRuleParser.php index 43a28db989d8..3af36ce8985e 100644 --- a/src/Illuminate/Validation/ValidationRuleParser.php +++ b/src/Illuminate/Validation/ValidationRuleParser.php @@ -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]; } diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index b0ee1f2e6454..bc89a7a6743a 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -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()); }