]> Raphaël G. Git Repositories - packbundle/commitdiff
Add captcha option master 0.5.0
authorRaphaël Gertz <git@rapsys.eu>
Tue, 2 Apr 2024 03:42:10 +0000 (05:42 +0200)
committerRaphaël Gertz <git@rapsys.eu>
Tue, 2 Apr 2024 03:42:10 +0000 (05:42 +0200)
Form/CaptchaType.php

index a2ecb878d1da3bd5e1ff9b0535a6bf028142328b..c3a5f69c4559e8d588ad40d62f5453dcc8f8c0a2 100644 (file)
@@ -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
         *