]> Raphaƫl G. Git Repositories - packbundle/blobdiff - Form/CaptchaType.php
Version 0.5.7
[packbundle] / Form / CaptchaType.php
index a2ecb878d1da3bd5e1ff9b0535a6bf028142328b..9d9db16e5d2a09eefd1cc09109c1dbe8ec0bf9e0 100644 (file)
@@ -11,6 +11,7 @@
 
 namespace Rapsys\PackBundle\Form;
 
 
 namespace Rapsys\PackBundle\Form;
 
+use Rapsys\PackBundle\RapsysPackBundle;
 use Rapsys\PackBundle\Util\ImageUtil;
 use Rapsys\PackBundle\Util\SluggerUtil;
 
 use Rapsys\PackBundle\Util\ImageUtil;
 use Rapsys\PackBundle\Util\SluggerUtil;
 
@@ -21,6 +22,7 @@ use Symfony\Component\Form\FormBuilderInterface;
 use Symfony\Component\Form\FormError;
 use Symfony\Component\Form\FormEvent;
 use Symfony\Component\Form\FormEvents;
 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;
 
 /**
 use Symfony\Contracts\Translation\TranslatorInterface;
 
 /**
@@ -35,8 +37,9 @@ class CaptchaType extends AbstractType {
         * @param ?ImageUtil $image The image instance
         * @param ?SluggerUtil $slugger The slugger instance
         * @param ?TranslatorInterface $translator The translator instance
         * @param ?ImageUtil $image The image instance
         * @param ?SluggerUtil $slugger The slugger instance
         * @param ?TranslatorInterface $translator The translator instance
+        * @param bool $enable Use captcha
         */
         */
-       public function __construct(protected ?ImageUtil $image = null, protected ?SluggerUtil $slugger = null, protected ?TranslatorInterface $translator = null) {
+       public function __construct(protected ?ImageUtil $image = null, protected ?SluggerUtil $slugger = null, protected ?TranslatorInterface $translator = null, protected bool $enable = false) {
        }
 
        /**
        }
 
        /**
@@ -46,21 +49,35 @@ class CaptchaType extends AbstractType {
         */
        public function buildForm(FormBuilderInterface $builder, array $options): void {
                //With image, slugger and translator
         */
        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
                        //Set captcha
-                       $captcha = $this->image->getCaptcha((new \DateTime('-1 year'))->getTimestamp());
+                       $captcha = $this->image->getCaptcha();
 
                        //Add captcha token
                        $builder->add('_captcha_token', HiddenType::class, ['data' => $captcha['token'], 'empty_data' => $captcha['token'], 'mapped' => false]);
 
                        //Add captcha
 
                        //Add captcha token
                        $builder->add('_captcha_token', HiddenType::class, ['data' => $captcha['token'], 'empty_data' => $captcha['token'], 'mapped' => false]);
 
                        //Add captcha
-                       $builder->add('captcha', IntegerType::class, ['label_attr' => ['class' => 'captcha'], 'label' => '<img src="'.htmlentities($captcha['src']).'" alt="'.htmlentities($captcha['equation']).'" />', 'label_html' => true, 'mapped' => false, 'translation_domain' => false]);
+                       $builder->add('captcha', IntegerType::class, ['label_attr' => ['class' => 'captcha'], 'label' => '<img src="'.htmlentities($captcha['src']).'" alt="'.htmlentities($captcha['equation']).'" />', 'label_html' => true, 'mapped' => false, 'translation_domain' => false, 'required' => true]);
 
                        //Add event listener on captcha
                        $builder->addEventListener(FormEvents::PRE_SUBMIT, [$this, 'validateCaptcha']);
                }
        }
 
 
                        //Add event listener on captcha
                        $builder->addEventListener(FormEvents::PRE_SUBMIT, [$this, 'validateCaptcha']);
                }
        }
 
+       /**
+        * {@inheritdoc}
+        */
+       public function configureOptions(OptionsResolver $resolver): void {
+               //Call parent configure options
+               parent::configureOptions($resolver);
+
+               //Set defaults
+               $resolver->setDefaults(['captcha' => $this->enable, 'error_bubbling' => true, 'translation_domain' => RapsysPackBundle::getAlias()]);
+
+               //Add extra captcha option
+               $resolver->setAllowedTypes('captcha', 'boolean');
+       }
+
        /**
         * Validate captcha
         *
        /**
         * Validate captcha
         *
@@ -79,6 +96,8 @@ class CaptchaType extends AbstractType {
                //Without captcha
                if (empty($data['captcha'])) {
                        //Add error on captcha
                //Without captcha
                if (empty($data['captcha'])) {
                        //Add error on captcha
+                       //XXX: we need to add error on form
+                       //XXX: see https://github.com/symfony/symfony/issues/35831
                        $form->addError(new FormError($this->translator->trans('Captcha is empty')));
 
                        //Reset captcha token
                        $form->addError(new FormError($this->translator->trans('Captcha is empty')));
 
                        //Reset captcha token
@@ -89,6 +108,8 @@ class CaptchaType extends AbstractType {
                //With invalid captcha
                } elseif ($this->slugger->hash($data['captcha']) !== $data['_captcha_token']) {
                        //Add error on captcha
                //With invalid captcha
                } elseif ($this->slugger->hash($data['captcha']) !== $data['_captcha_token']) {
                        //Add error on captcha
+                       //XXX: we need to add error on form
+                       //XXX: see https://github.com/symfony/symfony/issues/35831
                        $form->addError(new FormError($this->translator->trans('Captcha is invalid')));
 
                        //Reset captcha token
                        $form->addError(new FormError($this->translator->trans('Captcha is invalid')));
 
                        //Reset captcha token