src/Form/ReCaptchaType.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Form\EventListener\ReCaptchaValidationListener;
  4. use ReCaptcha\ReCaptcha;
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\FormBuilderInterface;
  7. use Symfony\Component\Form\FormInterface;
  8. use Symfony\Component\Form\FormView;
  9. use Symfony\Component\OptionsResolver\OptionsResolver;
  10. /**
  11.  * Class ReCaptchaType.
  12.  */
  13. class ReCaptchaType extends AbstractType
  14. {
  15.     /**
  16.      * @var ReCaptcha
  17.      */
  18.     private $reCaptcha;
  19.     /**
  20.      * ReCaptchaType constructor.
  21.      *
  22.      * @param ReCaptcha $reCaptcha
  23.      */
  24.     public function __construct(ReCaptcha $reCaptcha)
  25.     {
  26.         $this->reCaptcha $reCaptcha;
  27.     }
  28.     /**
  29.      * @inheritDoc
  30.      */
  31.     public function buildForm(FormBuilderInterface $builder, array $options)
  32.     {
  33.         $subscriber = new ReCaptchaValidationListener($this->reCaptcha);
  34.         $subscriber->setInvalidMessage($options['invalid_message']);
  35.         $builder->addEventSubscriber($subscriber);
  36.     }
  37.     /**
  38.      * @inheritDoc
  39.      */
  40.     public function buildView(FormView $viewFormInterface $form, array $options)
  41.     {
  42.         $view->vars['type'] = $options['type'];
  43.     }
  44.     /**
  45.      * @inheritDoc
  46.      */
  47.     public function configureOptions(OptionsResolver $resolver)
  48.     {
  49.         $resolver
  50.             ->setDefault('type''invisible')
  51.             ->setAllowedValues('type', ['checkbox''invisible']);
  52.         $resolver
  53.             ->setDefault('invalid_message''The captcha is invalid. Please try again.');
  54.     }
  55. }