]> Raphaël G. Git Repositories - packbundle/blob - Form/CaptchaType.php
Use bundle alias
[packbundle] / Form / CaptchaType.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys PackBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\PackBundle\Form;
13
14 use Rapsys\PackBundle\RapsysPackBundle;
15 use Rapsys\PackBundle\Util\ImageUtil;
16 use Rapsys\PackBundle\Util\SluggerUtil;
17
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;
27
28 /**
29 * Captcha Type class definition
30 *
31 * @see https://symfony.com/doc/current/form/create_custom_field_type.html
32 */
33 class CaptchaType extends AbstractType {
34 /**
35 * Constructor
36 *
37 * @param ?ImageUtil $image The image instance
38 * @param ?SluggerUtil $slugger The slugger instance
39 * @param ?TranslatorInterface $translator The translator instance
40 */
41 public function __construct(protected ?ImageUtil $image = null, protected ?SluggerUtil $slugger = null, protected ?TranslatorInterface $translator = null) {
42 }
43
44 /**
45 * {@inheritdoc}
46 *
47 * Build form
48 */
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) {
52 //Set captcha
53 $captcha = $this->image->getCaptcha();
54
55 //Add captcha token
56 $builder->add('_captcha_token', HiddenType::class, ['data' => $captcha['token'], 'empty_data' => $captcha['token'], 'mapped' => false]);
57
58 //Add captcha
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]);
60
61 //Add event listener on captcha
62 $builder->addEventListener(FormEvents::PRE_SUBMIT, [$this, 'validateCaptcha']);
63 }
64 }
65
66 /**
67 * {@inheritdoc}
68 */
69 public function configureOptions(OptionsResolver $resolver): void {
70 //Call parent configure options
71 parent::configureOptions($resolver);
72
73 //Set defaults
74 $resolver->setDefaults(['captcha' => false, 'translation_domain' => RapsysPackBundle::getAlias()]);
75
76 //Add extra captcha option
77 $resolver->setAllowedTypes('captcha', 'boolean');
78 }
79
80 /**
81 * Validate captcha
82 *
83 * @param FormEvent $event The form event
84 */
85 public function validateCaptcha(FormEvent $event): void {
86 //Get form
87 $form = $event->getForm();
88
89 //Get event data
90 $data = $event->getData();
91
92 //Set token
93 $token = $form->get('_captcha_token')->getConfig()->getData();
94
95 //Without captcha
96 if (empty($data['captcha'])) {
97 //Add error on captcha
98 $form->addError(new FormError($this->translator->trans('Captcha is empty')));
99
100 //Reset captcha token
101 $data['_captcha_token'] = $token;
102
103 //Set event data
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')));
109
110 //Reset captcha token
111 $data['_captcha_token'] = $token;
112
113 //Set event data
114 $event->setData($data);
115 }
116 }
117 }