src/Form/PasswordType.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\User;
  4. use Symfony\Component\Form\AbstractType;
  5. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  6. use Symfony\Component\Form\FormBuilderInterface;
  7. use Symfony\Component\OptionsResolver\OptionsResolver;
  8. use Symfony\Component\Security\Core\Security;
  9. use Symfony\Component\Validator\Constraints\Length;
  10. use Symfony\Component\Validator\Constraints\NotBlank;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. class PasswordType extends AbstractType
  13. {
  14.     /**
  15.      * @var Security
  16.      */
  17.     private $security;
  18.     private TranslatorInterface $translator;
  19.     public function __construct(Security $securityTranslatorInterface $translator)
  20.     {
  21.         $this->security $security;
  22.         $this->translator $translator;
  23.     }
  24.     public function buildForm(FormBuilderInterface $builder, array $options): void
  25.     {
  26.         $_ $this->translator;
  27.         $builder
  28.             ->add('password'RepeatedType::class, [
  29.                 'type' => \Symfony\Component\Form\Extension\Core\Type\PasswordType::class,
  30.                 'invalid_message' => $_->trans('Password.invalidMessage'),
  31.                 'options' => ['attr' => ['class' => 'form-control']],
  32.                 'required' => false,
  33.                 'empty_data' => null,
  34.                 'first_options' => [
  35.                     'label' => "Password.first",
  36.                     'attr' => [
  37.                         'placeHolder' => $_->trans('select.password'),
  38.                         'autocomplete' => 'new-password',
  39.                     ],
  40.                     'constraints' => [
  41.                         new NotBlank([
  42.                             'message' => $_->trans('Password.pleaseEnterPassword'),
  43.                         ]),
  44.                         new Length([
  45.                             'min' => 6,
  46.                             'minMessage' => $_->trans('Password.minValidation'),
  47.                             // max length allowed by Symfony for security reasons
  48.                             'max' => 4096,
  49.                         ]),
  50.                     ],
  51.                 ],
  52.                 'second_options' => ['label' => 'Password.second''attr' => ['placeHolder' => $_->trans('select.repeatPassword')],
  53.                     'constraints' => [
  54.                         new NotBlank([
  55.                             'message' => $_->trans('Password.pleaseEnterPassword'),
  56.                         ]),
  57.                         new Length([
  58.                             'min' => 6,
  59.                             'minMessage' => $_->trans('Password.minValidation'),
  60.                             // max length allowed by Symfony for security reasons
  61.                             'max' => 4096,
  62.                         ]),
  63.                     ],
  64.                 ],
  65.             ])
  66.         ;
  67.     }
  68.     public function configureOptions(OptionsResolver $resolver): void
  69.     {
  70.         $resolver->setDefaults([
  71.             'data_class' => User::class,
  72.         ]);
  73.     }
  74. }