symfony 3使用'strict'=>在电子邮件验证字段中为true

时间:2018-02-19 22:39:08

标签: forms symfony validation email

我想在表单构建中将'strict' => true添加到电子邮件验证输入中 https://symfony.com/doc/3.3/reference/constraints/Email.html#strict

为什么呢?因为普通电子邮件验证允许无效的电子邮件。 test@test通过了!

我试过,但我收到错误(The option "strict" does not exist. Defined options are: "action", "allow_extra_fields", "attr", ... )

这样的代码:

public function buildForm(FormBuilderInterface $builder, array $options) :void
{
     $builder
     ->add('name', TextType::class, [ 'label' => 'Name' ])
     ->add('email', EmailType::class, [
           'label' => 'Email', 
           'strict' => true ]  <<<<< not work
     )
     ->add('subject', ChoiceType::class, [ 'label' => 'Subject',
     ...

2 个答案:

答案 0 :(得分:2)

尝试注册以下验证限制:

$builder
     ->add('name', TextType::class, [ 'label' => 'Name' ])
     ->add('email', EmailType::class, [
           'label' => 'Email', 
           'constraints' => array(
               new Email(array('strict'=>true)),
            )
       )

答案 1 :(得分:0)

除了将'strict' => true添加到Email约束选项之外,Symfony框架配置中还存在一个设置,以默认情况下为所有Email约束启用它。参见strict_email configuration in the Symfony docs v3.4

对于Symfony> = 4.1,配置选项strict_email已弃用so use email_validation_mode instead