Last active
July 22, 2020 10:07
-
-
Save webdevilopers/31c8e39d786f2e02ecb2321f9c2486ac to your computer and use it in GitHub Desktop.
Symfony Validator Constraints Regex behaves differently to PHP preg_match condition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Acme\PersonnelManagement\Application\Service\Person; | |
| use Acme\Common\Domain\Model\FirstName; | |
| use Symfony\Component\Validator\Constraints as Assert; | |
| final class ChangeName | |
| { | |
| /** | |
| * @var FirstName | |
| * @Assert\NotNull | |
| * @Assert\NotBlank | |
| * @Assert\Type(type="string") | |
| * @Assert\Regex(pattern="/^[\p{L}\\-\\. \']+$/u") | |
| */ | |
| private $firstName; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Acme\Common\Domain\Model\PersonalName; | |
| final class NamePolicy | |
| { | |
| public static function isSatisfiedBy(string $name): bool | |
| { | |
| if (empty($name)) { | |
| return false; | |
| } | |
| // Allowed are unicode letters only and `.`, `-`, `'`, no numbers. | |
| if (0 === preg_match("/^[\p{L}\\-\\. \']+$/u", $name)) { | |
| return false; | |
| } | |
| // Continuous uppercase letters are not allowed. | |
| if (1 === preg_match("/[\p{Lu}]{2,}/u", $name)) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Tests\Acme\Common\Domain\PersonalName; | |
| use PHPUnit\Framework\TestCase; | |
| use Acme\Common\Domain\Model\PersonalName\NamePolicy; | |
| final class NamePolicyTest extends TestCase | |
| { | |
| /** @test */ | |
| public function hyphenSeparatedDoubleNamesAreAllowed(): void | |
| { | |
| $policy = NamePolicy::isSatisfiedBy('Hans-Werner'); | |
| $this->assertTrue($policy); | |
| } | |
| } |
Author
Author
It looks like you have to remove the "double escape" as mentioned in:
- symfony/symfony#22865
- https://stackoverflow.com/questions/44126803/symfony-regex-validator-doesnt-work-in-annotation
BEFORE:
/**
* @var FirstName
* @Assert\NotNull
* @Assert\NotBlank
* @Assert\Type(type="string")
* @Assert\Regex(pattern="/^[\p{L}\\-\\. \']+$/u")
*/AFTER:
/**
* @var FirstName
* @Assert\NotNull
* @Assert\NotBlank
* @Assert\Type(type="string")
* @Assert\Regex(pattern="/^[\p{L}\-\. \']+$/u")
*/Thank you @lashae & @javiereguiluz !
Author
In addition it looks like the "double escape" in my original pattern is not required at all.
if (1 !== preg_match("/^[\p{L}\-\. \']+$/u", $name, $matches)) {
return false;
}Works fine.
Author
Improved by @freshp:
// Allowed are unicode letters only and `.`, `-`, `'`, no numbers.
if (1 !== preg_match("/^[\p{L}\-\.\s\']+$/u", $name)) {
return false;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Came from:
The regex pattern for the
NamePolicyl. 14 matches the asserted pattern inChangeNamecommand l. 14.It works as expected in both cases for:
But not for "Hans-Werner"! The command the symfony constraint throws an error:
In how far does the Symfony Validator Constraint handle the pattern differently from
preg_match?