【问题标题】:symfony 3 use 'strict' => true in email validation fieldsymfony 3 在电子邮件验证字段中使用 'strict' => true
【发布时间】:2018-07-30 05:22:22
【问题描述】:

我想将'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',
     ...

【问题讨论】:

    标签: forms symfony validation email


    【解决方案1】:

    尝试注册验证约束,如下所示:

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

    【讨论】:

    • 好的,但是电子邮件输入仍然认为test@test 是正确的验证电子邮件!有什么想法吗?
    • 在这种情况下你必须使用自定义模式。 Regex
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 2017-09-25
      • 1970-01-01
      • 2011-03-06
      • 2021-11-27
      相关资源
      最近更新 更多