* @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) {
}
/**
$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']);
parent::configureOptions($resolver);
//Set defaults
- $resolver->setDefaults(['captcha' => false, 'translation_domain' => RapsysPackBundle::getAlias()]);
+ $resolver->setDefaults(['captcha' => $this->enable, 'error_bubbling' => true, 'translation_domain' => RapsysPackBundle::getAlias()]);
//Add extra captcha option
$resolver->setAllowedTypes('captcha', 'boolean');
//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
//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