diff --git a/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php b/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php index d76710fba89cc..b14d73a4a6adc 100755 --- a/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php +++ b/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php @@ -261,7 +261,7 @@ public function guessPatternForConstraint(Constraint $constraint) return new ValueGuess(sprintf('.{%s,%s}', (string) $constraint->min, (string) $constraint->max), Guess::LOW_CONFIDENCE); case 'Symfony\Component\Validator\Constraints\Regex': - return new ValueGuess($constraint->pattern, Guess::HIGH_CONFIDENCE ); + return new ValueGuess($constraint->getNonDelimitedPattern(), Guess::HIGH_CONFIDENCE ); case 'Symfony\Component\Validator\Constraints\Min': return new ValueGuess(sprintf('.{%s,}', strlen((string) $constraint->limit)), Guess::LOW_CONFIDENCE); diff --git a/src/Symfony/Component/Form/Tests/FormFactoryTest.php b/src/Symfony/Component/Form/Tests/FormFactoryTest.php index b3dd506495771..189cdeec8cb32 100644 --- a/src/Symfony/Component/Form/Tests/FormFactoryTest.php +++ b/src/Symfony/Component/Form/Tests/FormFactoryTest.php @@ -528,7 +528,7 @@ public function testCreateBuilderUsesPatternIfFound() ->method('guessPattern') ->with('Application\Author', 'firstName') ->will($this->returnValue(new ValueGuess( - '/[a-z]/', + '[a-z]', Guess::MEDIUM_CONFIDENCE ))); @@ -536,7 +536,7 @@ public function testCreateBuilderUsesPatternIfFound() ->method('guessPattern') ->with('Application\Author', 'firstName') ->will($this->returnValue(new ValueGuess( - '/[a-zA-Z]/', + '[a-zA-Z]', Guess::HIGH_CONFIDENCE ))); @@ -544,7 +544,7 @@ public function testCreateBuilderUsesPatternIfFound() $factory->expects($this->once()) ->method('createNamedBuilder') - ->with('firstName', 'text', null, array('pattern' => '/[a-zA-Z]/')) + ->with('firstName', 'text', null, array('pattern' => '[a-zA-Z]')) ->will($this->returnValue('builderInstance')); $builder = $factory->createBuilderForProperty( diff --git a/src/Symfony/Component/Validator/Constraints/Regex.php b/src/Symfony/Component/Validator/Constraints/Regex.php index 73f8b7b3323df..91395d83ee949 100644 --- a/src/Symfony/Component/Validator/Constraints/Regex.php +++ b/src/Symfony/Component/Validator/Constraints/Regex.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Validator\Constraints; use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\Exception\ConstraintDefinitionException; /** * @Annotation @@ -39,4 +40,19 @@ public function getRequiredOptions() { return array('pattern'); } + + /** + * Sometimes, like when converting to HTML5 pattern attribute, the regex is needed without the delimiters + * Example: /[a-z]+/ would be converted to [a-z]+ + * However, if options are specified, it cannot be converted and this will throw an Exception + * @return string regex + * @throws Symfony\Component\Validator\Exception\ConstraintDefinitionException + */ + public function getNonDelimitedPattern() { + if (preg_match('/^(.)(.*)\1$/', $this->pattern, $matches)) { + return $matches[2]; + } else { + throw new ConstraintDefinitionException("Cannot remove delimiters from pattern '{$this->pattern}'."); + } + } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php index 755f488577851..6ce21e9c9ae63 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php @@ -113,4 +113,21 @@ public function testConstraintGetDefaultOption() $this->assertEquals('pattern', $constraint->getDefaultOption()); } + + public function testNonDelimitedPattern() { + $constraint = new Regex(array( + 'pattern' => '/^[0-9]+$/', + )); + + $this->assertEquals('^[0-9]+$', $constraint->getNonDelimitedPattern()); + } + + public function testNonDelimitedPatternError() { + $constraint = new Regex(array( + 'pattern' => '/^[0-9]+$/i', + )); + + $this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException'); + $constraint->getNonDelimitedPattern(); + } }