src/Component/Captcha/EventSubscriber/CaptchaEventSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Component\Captcha\EventSubscriber;
  4. use App\Component\Captcha\CaptchaFactory;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\Form\FormError;
  7. use Symfony\Component\Form\FormEvent;
  8. use Symfony\Component\Form\FormEvents;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. class CaptchaEventSubscriber implements EventSubscriberInterface
  11. {
  12.     public function __construct(
  13.         private readonly CaptchaFactory $factory,
  14.         private readonly TranslatorInterface $translator
  15.     ) {
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             FormEvents::POST_SUBMIT => 'validateCaptcha',
  21.         ];
  22.     }
  23.     public function validateCaptcha(FormEvent $formEvent): void
  24.     {
  25.         if (!$this->factory->create()->isSuccess()) {
  26.             $formEvent->getForm()->addError(
  27.                 new FormError($this->translator->trans(id'captcha.message_invalid'domain'messages'))
  28.             );
  29.         }
  30.     }
  31. }