Julien Dargelos
Julien Dargelos

Reputation: 395

Symfony doesn't take my Length Assert in account

I'm writing validations on Symfony for a User Entity which has a password attribute.

namespace AppBundle\Entity;

...

class User implements UserInterface {
    
    ...
    
    /**
    * @ORM\Column(type="string")
    */
    private $password;

    /**
    * @Assert\NotBlank(message="Veuillez indiquer un mot de passe")
    * @Assert\Length(
    *   min=8,
    *   max=4096,
    *   minMessage="Votre mot de passe doit contenir au moins {{limit}} cacactères",
    *   maxMessage="Votre mot de passe ne doit pas contenir plus de {{limit}} caractères"
    * )
    */
    private $plain_password;
    
    ...
    
}

As you can see, I prevent the password from being empty and I check that its length is greater or equal to 8 characters.

But in addition, in the signup form I added a password confirmation thanks to the "Repeated" type provided by Symfony.

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

class UserFormType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
            ->add('email', EmailType::class)
            ->add('plain_password', RepeatedType::class, [
                'type' => PasswordType::class,
                'first_name' => 'first',
                'second_name' => 'confirmation',
                'invalid_message' => 'La confirmation ne correspond pas avec le mot de passe'
            ])
            ->add('firstname')
            ->add('lastname')
            ->add('city')
            ->add('website', UrlType::class)
            ->add('facebook')
            ->add('twitter')
            ->add('instagram')
            ->add('avatar')
            ->add('submit', SubmitType::class);
    }

    public function configureOptions(OptionsResolver $resolver) {
        $resolver->setDefaults([
            'data_class' => 'AppBundle\Entity\User'
        ]);
    }
}

Now, when I submit the form leaving the password field blank, I get the proper message "Veuillez indiquer un mot de passe".

When I submit the form with different values in the two password fields, I get also the proper message "La confirmation ne correspond pas avec le mot de passe".

But, when I submit the form with equal and not blank values in the two password fields, but with a length lower than 8; I do not get* any error message and the user is peacefully saved.


EDIT:

Alsatian: I don't have validation group set for the form

Controller action:

public function signupAction(Request $request) {
    $user = new User();
    $form = $this->createForm(UserFormType::class, $user);
    $form->handleRequest($request);

    if($form->isSubmitted() && $form->isValid()) {
        
        $em = $this->getDoctrine()->getManager();
        $em->persist($user);
        $em->flush();
        
        return $this->redirectToRoute('home');
    }

    return $this->render('user/signup.html.twig', [
        'form' => $form->createView()
    ]);
}

Upvotes: 3

Views: 2376

Answers (0)

Related Questions