src/Form/PasswordForgetType.php line 18

Open in your IDE?
  1. <?php
  2. //  src/Form/PasswordForgetType.php
  3. namespace App\Form;
  4. use Sulu\Bundle\CommunityBundle\Validator\Constraints\Exist;
  5. use Sulu\Bundle\SecurityBundle\Entity\User;
  6. use Symfony\Component\Form\AbstractType;
  7. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  8. use Symfony\Component\Form\Extension\Core\Type\TextType;
  9. use Symfony\Component\Form\FormBuilderInterface;
  10. use Symfony\Component\Form\FormEvent;
  11. use Symfony\Component\Form\FormEvents;
  12. use Symfony\Component\Form\FormInterface;
  13. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  14. use Symfony\Component\OptionsResolver\OptionsResolver;
  15. class PasswordForgetType extends AbstractType
  16. {
  17.     private $flashbag;
  18.     public function __construct(FlashBagInterface $flashbag)
  19.     {
  20.         $this->flashbag $flashbag;
  21.     }
  22.     /**
  23.      * {@inheritdoc}
  24.      */
  25.     public function buildForm(FormBuilderInterface $builder, array $options)
  26.     {
  27.         $builder->add('email_username'TextType::class, [
  28.             'constraints' => new Exist([
  29.                 'columns' => ['email''username'],
  30.                 'entity' => $options['user_class'],
  31.                 'groups' => 'password_forget'
  32.             ]),
  33.             'label_attr' => ["class" => "fs14 fw400 lh24"],
  34.             'label' => "Email *",
  35.             'attr' => ['placeholder' => 'you@example.com'],
  36.             'required' => true
  37.         ]);
  38.         $builder->add('submit'SubmitType::class, [
  39.             'label' => 'Valider'
  40.         ]);
  41.         $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
  42.             $form $event->getForm();
  43.             if (!$form->isValid()) {
  44.                 $this->addValidationErrorsToFlashBag($form$this->flashbag);
  45.             }
  46.         });
  47.     }
  48.     private function addValidationErrorsToFlashBag(FormInterface $formFlashBagInterface $flashBag)
  49.     {
  50.         $errors $form->getErrors(true);
  51.         foreach ($errors as $error) { 
  52.             $newErrorMessage '';
  53.             if ($error->getMessageTemplate() === "The value \"%string%\" was not found.") {
  54.                 $wrongEmail $error->getMessageParameters()["%string%"];
  55.                 $newErrorMessage "La valeur \"$wrongEmail\" n'existe pas.";
  56.             }
  57.             $flashBag->add('error'$newErrorMessage);
  58.         }
  59.     }
  60.     /**
  61.      * {@inheritdoc}
  62.      */
  63.     public function configureOptions(OptionsResolver $resolver)
  64.     {
  65.         $resolver->setDefaults([
  66.             'user_class' => User::class,
  67.             'validation_groups' => ['password_forget'],
  68.         ]);
  69.     }
  70. }