Search code examples
phpsymfonysymfony4

Symfony 4 Remove a default set constraint (notBlank) on a RepeatedType Password field


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


Solution

  • 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;