From 371738010baf06170c7b82495080884f9f782574 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Tue, 2 Apr 2024 05:42:10 +0200 Subject: [PATCH] Add captcha option --- Form/CaptchaType.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Form/CaptchaType.php b/Form/CaptchaType.php index a2ecb87..c3a5f69 100644 --- a/Form/CaptchaType.php +++ b/Form/CaptchaType.php @@ -21,6 +21,7 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormError; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; +use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Contracts\Translation\TranslatorInterface; /** @@ -46,7 +47,7 @@ class CaptchaType extends AbstractType { */ public function buildForm(FormBuilderInterface $builder, array $options): void { //With image, slugger and translator - if ($this->image !== null && $this->slugger !== null && $this->translator !== null) { + if (!empty($options['captcha']) && $this->image !== null && $this->slugger !== null && $this->translator !== null) { //Set captcha $captcha = $this->image->getCaptcha((new \DateTime('-1 year'))->getTimestamp()); @@ -61,6 +62,20 @@ class CaptchaType extends AbstractType { } } + /** + * {@inheritdoc} + */ + public function configureOptions(OptionsResolver $resolver): void { + //Call parent configure options + parent::configureOptions($resolver); + + //Set defaults + $resolver->setDefaults(['captcha' => false]); + + //Add extra captcha option + $resolver->setAllowedTypes('captcha', 'boolean'); + } + /** * Validate captcha * -- 2.41.1