1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys PackBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\PackBundle\Form
;
14 use Rapsys\PackBundle\RapsysPackBundle
;
15 use Rapsys\PackBundle\Util\ImageUtil
;
16 use Rapsys\PackBundle\Util\SluggerUtil
;
18 use Symfony\Component\Form\AbstractType
;
19 use Symfony\Component\Form\Extension\Core\Type\HiddenType
;
20 use Symfony\Component\Form\Extension\Core\Type\IntegerType
;
21 use Symfony\Component\Form\FormBuilderInterface
;
22 use Symfony\Component\Form\FormError
;
23 use Symfony\Component\Form\FormEvent
;
24 use Symfony\Component\Form\FormEvents
;
25 use Symfony\Component\OptionsResolver\OptionsResolver
;
26 use Symfony\Contracts\Translation\TranslatorInterface
;
29 * Captcha Type class definition
31 * @see https://symfony.com/doc/current/form/create_custom_field_type.html
33 class CaptchaType
extends AbstractType
{
37 * @param ?ImageUtil $image The image instance
38 * @param ?SluggerUtil $slugger The slugger instance
39 * @param ?TranslatorInterface $translator The translator instance
41 public function __construct(protected ?ImageUtil
$image = null, protected ?SluggerUtil
$slugger = null, protected ?TranslatorInterface
$translator = null) {
49 public function buildForm(FormBuilderInterface
$builder, array $options): void {
50 //With image, slugger and translator
51 if (!empty($options['captcha']) && $this->image
!== null && $this->slugger
!== null && $this->translator
!== null) {
53 $captcha = $this->image
->getCaptcha();
56 $builder->add('_captcha_token', HiddenType
::class, ['data' => $captcha['token'], 'empty_data' => $captcha['token'], 'mapped' => false]);
59 $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]);
61 //Add event listener on captcha
62 $builder->addEventListener(FormEvents
::PRE_SUBMIT
, [$this, 'validateCaptcha']);
69 public function configureOptions(OptionsResolver
$resolver): void {
70 //Call parent configure options
71 parent
::configureOptions($resolver);
74 $resolver->setDefaults(['captcha' => false, 'translation_domain' => RapsysPackBundle
::getAlias()]);
76 //Add extra captcha option
77 $resolver->setAllowedTypes('captcha', 'boolean');
83 * @param FormEvent $event The form event
85 public function validateCaptcha(FormEvent
$event): void {
87 $form = $event->getForm();
90 $data = $event->getData();
93 $token = $form->get('_captcha_token')->getConfig()->getData();
96 if (empty($data['captcha'])) {
97 //Add error on captcha
98 $form->addError(new FormError($this->translator
->trans('Captcha is empty')));
100 //Reset captcha token
101 $data['_captcha_token'] = $token;
104 $event->setData($data);
105 //With invalid captcha
106 } elseif ($this->slugger
->hash($data['captcha']) !== $data['_captcha_token']) {
107 //Add error on captcha
108 $form->addError(new FormError($this->translator
->trans('Captcha is invalid')));
110 //Reset captcha token
111 $data['_captcha_token'] = $token;
114 $event->setData($data);