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;
/**
/**
* Constructor
*
- * @param ?ImageUtil $image
- * @param ?SluggerUtil $slugger
+ * @param ?ImageUtil $image The image instance
+ * @param ?SluggerUtil $slugger The slugger instance
* @param ?TranslatorInterface $translator The translator instance
*/
public function __construct(protected ?ImageUtil $image = null, protected ?SluggerUtil $slugger = null, protected ?TranslatorInterface $translator = null) {
*/
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());
}
}
+ /**
+ * {@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
*