src/Form/ChangePasswordFormType.php line 14

  1. <?php
  2. namespace App\Form;
  3. use Symfony\Component\Form\AbstractType;
  4. use Symfony\Component\Form\FormBuilderInterface;
  5. use Symfony\Component\Validator\Constraints\Length;
  6. use Symfony\Component\Validator\Constraints\NotBlank;
  7. use Symfony\Component\OptionsResolver\OptionsResolver;
  8. use Symfony\Contracts\Translation\TranslatorInterface;
  9. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  10. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  11. class ChangePasswordFormType extends AbstractType
  12. {
  13.     public function __construct(private TranslatorInterface $translator)
  14.     {
  15.     }
  16.     public function buildForm(FormBuilderInterface $builder, array $options): void
  17.     {
  18.         $builder
  19.             ->add('plainPassword'RepeatedType::class, [
  20.                 'type' => PasswordType::class,
  21.                 'label'=>$this->translator->trans('Password'),
  22.                 'first_options' => [
  23.                     'attr' => ['autocomplete' => 'new-password''class' => 'form-control mb-4'],
  24.                     'constraints' => [
  25.                             new NotBlank([
  26.                                 'message' => $this->translator->trans('Veuillez entrer un mot de passe'),
  27.                             ]),
  28.                             new Length([
  29.                                 'min' => 6,
  30.                                 'minMessage' => $this->translator->trans('Votre mot de passe doit comporter au moins {{ limit }} caractères'),
  31.                                 // max length allowed by Symfony for security reasons
  32.                                 'max' => 4096,
  33.                             ]),
  34.                         ],
  35.                     'label' => $this->translator->trans('Nouveau mot de passe'),
  36.                 ],
  37.                 'second_options' => [
  38.                     'attr' => ['autocomplete' => 'new-password''class' => 'form-control'],
  39.                     'label' => $this->translator->trans('Répéter le mot de passe'),
  40.                 ],
  41.                 'invalid_message' => $this->translator->trans('Les champs du mot de passe doivent correspondre.'),
  42.                 // Instead of being set onto the object directly,
  43.                 // this is read and encoded in the controller
  44.                 'mapped' => false,
  45.             ])
  46.         ;
  47.     }
  48.     public function configureOptions(OptionsResolver $resolver): void
  49.     {
  50.         $resolver->setDefaults([]);
  51.     }
  52. }