Symfony Assert \ Regex没有跟preg_match比较

时间:2015-12-11 14:47:59

标签: php regex symfony

我正在使用Silex来验证一个简单的API。

对于POST登录,我使用symfony验证Assert类但是Assert \ Regex没有按计划运行。 这是我的约束:

$constraints = new Assert\Collection(
    array(
        'username' => new Assert\Required(
            array(
                new Assert\NotNull(), 
                new Assert\NotBlank(), 
                new Assert\Email(array('strict'=>true)), 
                new Assert\Regex(array('pattern'=>'/^([a-z0-9_\.-]+\@[\da-z\.-]+\.[a-z\.]{2,6})$/'))
            )
        ),
        'password' => new Assert\Required(array(new Assert\NotNull(), new Assert\NotBlank())),
    )
);

这不起作用,允许用户@ user'''通过

然而

 preg_match("/^([a-z0-9_\.-]+\@[\da-z\.-]+\.[a-z\.]{2,6})$/", $email, $matchResults);
 if (count($matchResults) === 0) {
    return false;
 }

preg_match等价物会捕获它并拒绝该电子邮件无效。

是否有一些我错过的Assert \ Regex将模式视为与preg_match不同?

0 个答案:

没有答案