vendor/nucleos/user-bundle/src/Form/Type/LoginFormType.php line 27

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the NucleosUserBundle package.
  4.  *
  5.  * (c) Christian Gripp <mail@core23.de>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Nucleos\UserBundle\Form\Type;
  11. use Symfony\Component\Form\AbstractType;
  12. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  13. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  14. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  15. use Symfony\Component\Form\Extension\Core\Type\TextType;
  16. use Symfony\Component\Form\FormBuilderInterface;
  17. use Symfony\Component\Form\FormError;
  18. use Symfony\Component\Form\FormEvent;
  19. use Symfony\Component\Form\FormEvents;
  20. use Symfony\Component\OptionsResolver\OptionsResolver;
  21. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  22. use Symfony\Contracts\Translation\TranslatorInterface;
  23. final class LoginFormType extends AbstractType
  24. {
  25.     /**
  26.      * @var AuthenticationUtils
  27.      */
  28.     private $authenticationUtils;
  29.     /**
  30.      * @var TranslatorInterface|null
  31.      */
  32.     private $translator;
  33.     public function __construct(AuthenticationUtils $authenticationUtils, ?TranslatorInterface $translator null)
  34.     {
  35.         $this->authenticationUtils $authenticationUtils;
  36.         $this->translator          $translator;
  37.     }
  38.     public function buildForm(FormBuilderInterface $builder, array $options): void
  39.     {
  40.         $builder
  41.             ->add('_username'TextType::class, [
  42.                 'label' => 'security.login.username',
  43.                 'attr'  => [
  44.                     'autocomplete' => 'username',
  45.                 ],
  46.             ])
  47.             ->add('_password'PasswordType::class, [
  48.                 'label' => 'security.login.password',
  49.                 'attr'  => [
  50.                     'autocomplete' => 'password',
  51.                 ],
  52.             ])
  53.             ->add('_remember_me'CheckboxType::class, [
  54.                 'label'    => 'security.login.remember_me',
  55.                 'required' => false,
  56.                 'value'    => 'on',
  57.             ])
  58.             ->add('_target_path'HiddenType::class)
  59.         ;
  60.         $authenticationUtils $this->authenticationUtils;
  61.         $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($authenticationUtils): void {
  62.             $error $authenticationUtils->getLastAuthenticationError();
  63.             if (null !== $error) {
  64.                 $message $error->getMessage();
  65.                 if (null !== $this->translator) {
  66.                     $message $this->translator->trans($message, [], 'NucleosUserBundle');
  67.                 }
  68.                 $event->getForm()->addError(new FormError($message));
  69.             }
  70.             $event->setData(array_replace((array) $event->getData(), [
  71.                 'username' => $authenticationUtils->getLastUsername(),
  72.             ]));
  73.         });
  74.     }
  75.     public function configureOptions(OptionsResolver $resolver): void
  76.     {
  77.         $resolver->setDefaults([
  78.             'translation_domain' => 'NucleosUserBundle',
  79.             'csrf_field_name'    => '_csrf_token',
  80.             'csrf_token_id'      => 'authenticate',
  81.         ]);
  82.     }
  83.     public function getBlockPrefix(): string
  84.     {
  85.         return '';
  86.     }
  87. }