Skip to content

[Form][Validator] Fixed generation of HTML5 pattern attribute based on Assert\Regex to remove delimiters. #4513

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

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Form/Tests/FormFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,23 +528,23 @@ public function testCreateBuilderUsesPatternIfFound()
->method('guessPattern')
->with('Application\Author', 'firstName')
->will($this->returnValue(new ValueGuess(
'/[a-z]/',
'[a-z]',
Guess::MEDIUM_CONFIDENCE
)));

$this->guesser2->expects($this->once())
->method('guessPattern')
->with('Application\Author', 'firstName')
->will($this->returnValue(new ValueGuess(
'/[a-zA-Z]/',
'[a-zA-Z]',
Guess::HIGH_CONFIDENCE
)));

$factory = $this->createMockFactory(array('createNamedBuilder'));

$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(
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;

/**
* @Annotation
Expand Down Expand Up @@ -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}'.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}