On déplace la gestion du contrôle du Captcha dans un controller spécifique --> c'est plus propre (merci Franck :-)

This commit is contained in:
2019-12-03 16:46:46 +01:00
parent 5d3943cd4b
commit 66758f160a
5 changed files with 45 additions and 21 deletions

View File

@@ -25,6 +25,11 @@
<tag name="kernel.event_subscriber" />
<argument type="service" id="request"/>
</service>
<service id="thelia.form_validator" class="ReCaptcha\Form\MyTheliaFormValidator">
<argument type="service" id="thelia.translator" />
<argument>%kernel.environment%</argument>
<argument type="service" id="event_dispatcher" />
</service>
</services>
<hooks>

View File

@@ -0,0 +1,39 @@
<?php
namespace ReCaptcha\Form;
use ReCaptcha\Event\ReCaptchaCheckEvent;
use ReCaptcha\Event\ReCaptchaEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Thelia\Form\BaseForm;
use Thelia\Form\Exception\FormValidationException;
class MyTheliaFormValidator extends \Thelia\Core\Form\TheliaFormValidator
{
/** @var EventDispatcherInterface */
protected $dispatcher;
public function __construct(TranslatorInterface $translator, $environment, EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
parent::__construct($translator, $environment);
}
public function validateForm(BaseForm $aBaseForm, $expectedMethod = null)
{
if ($aBaseForm->getRequest()->get('captcha')) {
$checkCaptchaEvent = new ReCaptchaCheckEvent();
$this->dispatcher->dispatch(ReCaptchaEvents::CHECK_CAPTCHA_EVENT, $checkCaptchaEvent);
if ($checkCaptchaEvent->isHuman() == false) {
throw new FormValidationException('Veuillez confirmer que vous n\'êtes pas un robot.');
}
}
return parent::validateForm($aBaseForm, $expectedMethod); // TODO: Change the autogenerated stub
}
}

View File

@@ -22,6 +22,6 @@ class FrontHook extends BaseHook
$captchaId = $captchaId.'-invisible';
}
$event->add("<div id='$captchaId' class='g-recaptcha' data-sitekey='$siteKey' $captchaCallback data-size='$captchaStyle'></div>");
$event->add("<div id='$captchaId' class='g-recaptcha' data-sitekey='$siteKey' $captchaCallback data-size='$captchaStyle'></div><input type='hidden' name='captcha' value='1'>");
}
}