Skip to content

Commit 00baeff

Browse files
authored
Merge pull request #38 from bvrbanec/improvement/captcha-error-message
Add error to form if captcha validation fails
2 parents 5e63b6e + 521fbb5 commit 00baeff

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

bundle/InformationCollectionTrait.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use eZ\Publish\Core\MVC\Symfony\View\LocationValueView;
77
use Netgen\Bundle\InformationCollectionBundle\Event\InformationCollected;
88
use Netgen\Bundle\InformationCollectionBundle\Form\Captcha\CaptchaValueInterface;
9+
use Symfony\Component\Form\FormError;
910
use Symfony\Component\HttpFoundation\Request;
1011

1112
trait InformationCollectionTrait
@@ -38,12 +39,12 @@ protected function collectInformation(ContentValueView $view, Request $request)
3839
->get('netgen_information_collection.factory.captcha')
3940
->getCaptcha($view->getLocation());
4041

41-
4242
$form->handleRequest($request);
43+
$validCaptcha = $captcha->isValid($request);
44+
$formSubmitted = $form->isSubmitted();
4345

44-
if ($form->isValid() && $form->isSubmitted() && $captcha->isValid($request)) {
46+
if ($formSubmitted && $form->isValid() && $validCaptcha) {
4547
$isValid = true;
46-
4748
$event = new InformationCollected($form->getData());
4849

4950
/** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher */
@@ -53,6 +54,10 @@ protected function collectInformation(ContentValueView $view, Request $request)
5354
$dispatcher->dispatch(Events::INFORMATION_COLLECTED, $event);
5455
}
5556

57+
if (true === $formSubmitted && false === $validCaptcha) {
58+
$form->addError(new FormError($this->container->get('translator')->trans('form.errors.captcha_failed', array(), 'netgen_information_collection_form_messages')));
59+
}
60+
5661
return array(
5762
'is_valid' => $isValid,
5863
'form' => $form->createView(),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
form.errors.captcha_failed: "Captcha validation failed"

0 commit comments

Comments
 (0)