dongxia5394 2018-09-27 12:28
浏览 32
已采纳

Symfony 4在RepeatedType密码字段上删除默认设置约束(notBlank)

I have form builder in a controller

$form = $this->createFormBuilder($user)

                 ->add('firstname',TextType::class,array(
                     'label' => 'First name',
                     'attr' => array(
                         'class' => 'form-control',
                         'value' => $user->getFirstname()
                     ),
                 ))
                 ->add('surname',TextType::class,array(
                     'label' => 'Last name',
                     'attr' => array(
                         'class' => 'form-control',
                         'value' => $user->getSurname()
                     )
                 ))
                 ->add('email', EmailType::class, array(
                     'label' => 'Email',
                     'attr' => array(
                         'class' => 'form-control',
                         'value' => $user->getEmail(),
                     ),
                 ))
                 ->add('plainPassword', RepeatedType::class, array(

                     'type' => PasswordType::class,
                     'invalid_message' => 'The password fields must match.',
                     'options' => array('attr' => array('class' => 
                        'password-field')),

                     'first_options'  => array('label' => 'Password'),
                     'second_options' => array('label' => 'Repeat 
                      Password'),
                 ))

As you can see on the plainPassword field there is no constraints set. My intention was to have the user not need to fill this field if they do not want to, however when I submit the form, the NotBlank constraint gets triggered and the form doesn't get submitted. I tried setting/adding, required to FALSE, doesn't work.

I even tried adding a new Blank() constraint and still it kept holding to the default constraint.

Is there a way to remove this default notBlankconstraint ??

Screengrab of the constraint kicking in enter image description here

  • 写回答

1条回答 默认 最新

  • doulizhi1247 2018-09-27 13:54
    关注

    Thanks to @AythaNzt's comment. So the Constraint asserted an an annotation on my 'user Entity' class

    It was this

     /**
     * @Assert\NotBlank()
     * @Assert\Length(max=4096)
     */
    private $plainPassword;
    

    Removed the NotBlank() So became

    /**
     * @Assert\Length(max=4096)
     */
    private $plainPassword;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊