symfony 中有一个电子邮件验证器,可以以以下形式使用:http://symfony.com/doc/current/reference/constraints/Email.html http://symfony.com/doc/current/reference/constraints/Email.html
我的问题是:如何在控制器中使用此验证器来验证电子邮件地址?
这可以通过使用 PHP preg_match for usere 来实现,但我的问题是是否有可能使用 Symfony 已经内置的电子邮件验证器。
先感谢您。
通过使用验证值 http://api.symfony.com/2.0/Symfony/Component/Validator/Validator.html#method_validateValue的方法验证器 http://api.symfony.com/2.0/Symfony/Component/Validator/Validator.html service
use Symfony\Component\Validator\Constraints\Email as EmailConstraint;
// ...
public function customAction()
{
$email = 'value_to_validate';
// ...
$emailConstraint = new EmailConstraint();
$emailConstraint->message = 'Your customized error message';
$errors = $this->get('validator')->validateValue(
$email,
$emailConstraint
);
// $errors is then empty if your email address is valid
// it contains validation error message in case your email address is not valid
// ...
}
// ...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)