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

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\HiddenType;
  13. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  14. use Symfony\Component\Form\Extension\Core\Type\TextType;
  15. use Symfony\Component\Form\FormBuilderInterface;
  16. use Symfony\Component\Form\FormError;
  17. use Symfony\Component\Form\FormEvent;
  18. use Symfony\Component\Form\FormEvents;
  19. use Symfony\Component\OptionsResolver\OptionsResolver;
  20. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  21. use Symfony\Contracts\Translation\TranslatorInterface;
  22. final class LoginFormType extends AbstractType
  23. {
  24.     private AuthenticationUtils $authenticationUtils;
  25.     private TranslatorInterface $translator;
  26.     public function __construct(AuthenticationUtils $authenticationUtilsTranslatorInterface $translator)
  27.     {
  28.         $this->authenticationUtils $authenticationUtils;
  29.         $this->translator          $translator;
  30.     }
  31.     public function buildForm(FormBuilderInterface $builder, array $options): void
  32.     {
  33.         $builder
  34.             ->add('_username'TextType::class, [
  35.                 'label' => 'security.login.username',
  36.                 'attr'  => [
  37.                     'autocomplete' => 'username',
  38.                 ],
  39.             ])
  40.             ->add('_password'PasswordType::class, [
  41.                 'label' => 'security.login.password',
  42.                 'attr'  => [
  43.                     'autocomplete' => 'password',
  44.                 ],
  45.             ])
  46.             ->add('_target_path'HiddenType::class)
  47.         ;
  48.         $authenticationUtils $this->authenticationUtils;
  49.         $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($authenticationUtils): void {
  50.             $error $authenticationUtils->getLastAuthenticationError();
  51.             if (null !== $error) {
  52.                 $message $this->translator->trans($error->getMessage(), [], 'NucleosUserBundle');
  53.                 $event->getForm()->addError(new FormError($message));
  54.             }
  55.             $event->setData(array_replace((array) $event->getData(), [
  56.                 'username' => $authenticationUtils->getLastUsername(),
  57.             ]));
  58.         });
  59.     }
  60.     public function configureOptions(OptionsResolver $resolver): void
  61.     {
  62.         $resolver->setDefaults([
  63.             'translation_domain' => 'NucleosUserBundle',
  64.             'csrf_field_name'    => '_csrf_token',
  65.             'csrf_token_id'      => 'authenticate',
  66.         ]);
  67.     }
  68.     public function getBlockPrefix(): string
  69.     {
  70.         return '';
  71.     }
  72. }