-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form][Validator] Fixed generation of HTML5 pattern attribute based on Assert\Regex by removing delimiters or using a new option: html_pattern. #4520
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
Changes from all commits
f81a9af
d29ac23
d2b2885
1077519
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
namespace Symfony\Component\Validator\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; | ||
|
||
/** | ||
* @Annotation | ||
|
@@ -22,6 +23,7 @@ class Regex extends Constraint | |
{ | ||
public $message = 'This value is not valid.'; | ||
public $pattern; | ||
public $html_pattern = null; | ||
public $match = true; | ||
|
||
/** | ||
|
@@ -39,4 +41,38 @@ 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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Brace in new line. Same below. |
||
if (preg_match('/^(.)(.*)\1$/', $this->pattern, $matches)) { | ||
$delimiter = $matches[1]; | ||
// Unescape the delimiter in pattern | ||
$pattern = str_replace('\\' . $delimiter, $delimiter, $matches[2]); | ||
return $pattern; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing new line before |
||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for else as you have |
||
throw new ConstraintDefinitionException("Cannot remove delimiters from pattern '{$this->pattern}'."); | ||
} | ||
} | ||
|
||
public function getHtmlPattern() { | ||
// If html_pattern is specified, use it | ||
if (!is_null($this->html_pattern)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return empty($this->html_pattern) | ||
? false | ||
: $this->html_pattern; | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again no need for |
||
try { | ||
return $this->getNonDelimitedPattern(); | ||
} catch (ConstraintDefinitionException $e) { | ||
// Pattern cannot be converted | ||
return false; | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,4 +113,64 @@ public function testConstraintGetDefaultOption() | |
|
||
$this->assertEquals('pattern', $constraint->getDefaultOption()); | ||
} | ||
|
||
public function testNonDelimitedPattern() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the curly brace should be on its own line for all methods There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually fixed in PR #4522, my bad for creating multiple PR, I just found out how to add commits to existing PR. |
||
$constraint = new Regex(array( | ||
'pattern' => '/^[0-9]+$/', | ||
)); | ||
|
||
$this->assertEquals('^[0-9]+$', $constraint->getNonDelimitedPattern()); | ||
} | ||
|
||
public function testNonDelimitedPatternEscaping() { | ||
$constraint = new Regex(array( | ||
'pattern' => '/^[0-9]+\/$/', | ||
)); | ||
|
||
$this->assertEquals('^[0-9]+/$', $constraint->getNonDelimitedPattern()); | ||
|
||
$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(); | ||
} | ||
|
||
public function testHtmlPattern() { | ||
// Specified html_pattern | ||
$constraint = new Regex(array( | ||
'pattern' => '/^[a-z]+$/i', | ||
'html_pattern' => '^[a-zA-Z]+$', | ||
)); | ||
$this->assertEquals('^[a-zA-Z]+$', $constraint->getHtmlPattern()); | ||
|
||
// Disabled html_pattern | ||
$constraint = new Regex(array( | ||
'pattern' => '/^[a-z]+$/i', | ||
'html_pattern' => false, | ||
)); | ||
$this->assertFalse($constraint->getHtmlPattern()); | ||
|
||
// Cannot be converted | ||
$constraint = new Regex(array( | ||
'pattern' => '/^[a-z]+$/i', | ||
)); | ||
$this->assertFalse($constraint->getHtmlPattern()); | ||
|
||
// Automaticaly converted | ||
$constraint = new Regex(array( | ||
'pattern' => '/^[a-z]+$/', | ||
)); | ||
$this->assertEquals('^[a-z]+$', $constraint->getHtmlPattern()); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be
$htmlPattern
.