]> Raphaƫl G. Git Repositories - packbundle/blobdiff - Form/CaptchaType.php
Allow captcha to be enabled in form service definition
[packbundle] / Form / CaptchaType.php
index c3a5f69c4559e8d588ad40d62f5453dcc8f8c0a2..8b0a872654ca6632948307ea80008540393ac539 100644 (file)
@@ -11,6 +11,7 @@
 
 namespace Rapsys\PackBundle\Form;
 
+use Rapsys\PackBundle\RapsysPackBundle;
 use Rapsys\PackBundle\Util\ImageUtil;
 use Rapsys\PackBundle\Util\SluggerUtil;
 
@@ -36,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 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) {
        }
 
        /**
@@ -49,7 +51,7 @@ class CaptchaType extends AbstractType {
                //With image, slugger and translator
                if (!empty($options['captcha']) && $this->image !== null && $this->slugger !== null && $this->translator !== null) {
                        //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]);
@@ -70,7 +72,7 @@ class CaptchaType extends AbstractType {
                parent::configureOptions($resolver);
 
                //Set defaults
-               $resolver->setDefaults(['captcha' => false]);
+               $resolver->setDefaults(['captcha' => $this->enable, 'error_bubbling' => true, 'translation_domain' => RapsysPackBundle::getAlias()]);
 
                //Add extra captcha option
                $resolver->setAllowedTypes('captcha', 'boolean');