Gregwar / Captcha

PHP Captcha library
MIT License
1.73k stars 290 forks source link

How to get the error generated by a wrongly filled Captcha? #22

Closed gallelloit closed 9 years ago

gallelloit commented 9 years ago

Hello. I have the bundle already working in Symfony2.0 (having installed the 2.0 version of the bundle). It is apparently working fine, except for a simple question.

When I type a wrong captcha, just to test the behavior and give a proper message, I am finding that the form doesn't gives any error.

When I do

 $process = $formHandler->process($confirmationEnabled);

I get a FALSE, so the form won't succeed. Tat is correct, as I typed wrongly the captcha text.

Nevertheless, after that, when I try to catch the given error

$errors = $form->getErrors();

I am getting an empty array, and so no message is coming and no message is being shown in the visual layer. I don't think that is the normal behavior.

But the weirdest part comes now:

When I inspect the form, I get no errors. That might be expected because $form->getErrors() returned an empty array, as I said before. But when I inspect the form children, I clearly can see how the captcha child HAS the error indeed. Again, this is expected, because the captcha is avoiding the form to be successfully processed. But the question is: How do I get the "internal child" error I got in the captcha child?

Attaching two images. One with the "no errors" father form. And the other with de error captcha child.

form no errors children errors

Thank you in advance!!

gallelloit commented 9 years ago

Ok, after debugging the bundle and reaching CaptchaValidator.php, I did something that actually solves my problem. I don't know if it is a proper way to do it, but it did it for me.

I changed the line where the validation error is added to the form errors array. It's not added anymore to the Captcha form, but to its parent instead:

//$form->addError(new FormError($this->invalidMessage));
$form->getParent()->addError(new FormError($this->invalidMessage));