X-Git-Url: https://git.rapsys.eu/packbundle/blobdiff_plain/1e261bd3495a7b730972d5a903de6b2ecc9cc1e8..8b3e5065464a5734fb7259b71c2d64b7cc463699:/Form/CaptchaType.php diff --git a/Form/CaptchaType.php b/Form/CaptchaType.php index d479b47..f8cf3d0 100644 --- a/Form/CaptchaType.php +++ b/Form/CaptchaType.php @@ -11,17 +11,19 @@ namespace Rapsys\PackBundle\Form; +use Rapsys\PackBundle\RapsysPackBundle; use Rapsys\PackBundle\Util\ImageUtil; use Rapsys\PackBundle\Util\SluggerUtil; -use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\IntegerType; use Symfony\Component\Form\FormBuilderInterface; -use Symfony\Component\Form\FormEvents; -use Symfony\Component\Form\FormEvent; 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; /** * Captcha Type class definition @@ -30,56 +32,50 @@ use Symfony\Component\Form\FormError; */ class CaptchaType extends AbstractType { /** - * @var ImageUtil $image - */ - private $image; - - /** - * @var SluggerUtil $slugger - */ - private $slugger; - - /** - * @var Translator instance + * Constructor + * + * @param ?ImageUtil $image The image instance + * @param ?SluggerUtil $slugger The slugger instance + * @param ?TranslatorInterface $translator The translator instance */ - private $translator; + public function __construct(protected ?ImageUtil $image = null, protected ?SluggerUtil $slugger = null, protected ?TranslatorInterface $translator = null) { + } /** - * Constructor + * {@inheritdoc} * - * @param ImageUtil $image - * @param SluggerUtil $slugger - * @param TranslatorInterface $translator The translator instance + * Build form */ - public function __construct(ImageUtil $image, SluggerUtil $slugger, TranslatorInterface $translator) { - //Set image - $this->image = $image; + public function buildForm(FormBuilderInterface $builder, array $options): void { + //With image, slugger and translator + if (!empty($options['captcha']) && $this->image !== null && $this->slugger !== null && $this->translator !== null) { + //Set captcha + $captcha = $this->image->getCaptcha(); - //Set slugger - $this->slugger = $slugger; + //Add captcha token + $builder->add('_captcha_token', HiddenType::class, ['data' => $captcha['token'], 'empty_data' => $captcha['token'], 'mapped' => false]); - //Set translator - $this->translator = $translator; + //Add captcha + $builder->add('captcha', IntegerType::class, ['label_attr' => ['class' => 'captcha'], 'label' => ''.htmlentities($captcha['equation']).'', 'label_html' => true, 'mapped' => false, 'translation_domain' => false]); + + //Add event listener on captcha + $builder->addEventListener(FormEvents::PRE_SUBMIT, [$this, 'validateCaptcha']); + } } /** - * Build form - * * {@inheritdoc} */ - public function buildForm(FormBuilderInterface $builder, array $options) { - //Set captcha - $captcha = $this->image->getCaptcha((new \DateTime('-1 year'))->getTimestamp()); - - //Add captcha token - $builder->add('_captcha_token', HiddenType::class, ['data' => $captcha['token'], 'empty_data' => $captcha['token']]); + public function configureOptions(OptionsResolver $resolver): void { + //Call parent configure options + parent::configureOptions($resolver); - //Add captcha - $builder->add('captcha', IntegerType::class, ['label_attr' => ['class' => 'captcha'], 'label' => ''.htmlentities($captcha['equation']).'', 'label_html' => true, 'translation_domain' => false]); + //Set defaults + $resolver->setDefaults(['captcha' => false, 'error_bubbling' => true, 'translation_domain' => RapsysPackBundle::getAlias()]); - //Add event listener on captcha - $builder->addEventListener(FormEvents::PRE_SUBMIT, [$this, 'validateCaptcha']); - } + //Add extra captcha option + $resolver->setAllowedTypes('captcha', 'boolean'); + } /** * Validate captcha