66use eZ \Publish \Core \MVC \Symfony \View \LocationValueView ;
77use Netgen \Bundle \InformationCollectionBundle \Event \InformationCollected ;
88use Netgen \Bundle \InformationCollectionBundle \Form \Captcha \CaptchaValueInterface ;
9+ use Symfony \Component \Form \FormError ;
910use Symfony \Component \HttpFoundation \Request ;
1011
1112trait 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 (),
0 commit comments