CaptchaBundle icon indicating copy to clipboard operation
CaptchaBundle copied to clipboard

incorrect captcha value - can't see error message if form is invalid

Open kirill-oficerov opened this issue 11 years ago • 6 comments

UserType class has code:

    $builder->add('captcha', 'captcha', array(
        'label' => 'user.add.label.captcha',
        'bypass_code' => 123
    ));

Controller's code:

...
$user = new User();
$form = $this->createForm(new UserType(), $user);
...
if($form->isValid()) {
} else {
    $errorsRaw = $this->get('validator')->validate($form);
}

$form is invalid here and i get no errors in array $errorsRaw if i type an incorrect captcha. But there are errors in the array if i type incorrect value to another field of the form. And that is the problem - I expect the captcha error to be appear in the $errorsRaw after validate() call.

Below is dirty solution - get all the form errors by traversing all the childs of the form:

public function getErrors($form) {
    $toReturn = array();
    foreach($form->all() as $name => $child) {
        if(!$child->isValid()) {
            $errors = $child->getErrors();
            $errorsToReturn = array();
            foreach($errors as $error) {
                $errorsToReturn[] = $error->getMessage();
            }
            $toReturn[$name] = $errorsToReturn;
        }
    }
    return $toReturn;
}

kirill-oficerov avatar Nov 04 '14 09:11 kirill-oficerov

Hello Do you correctly bind your form to the request?

Gregwar avatar Nov 05 '14 22:11 Gregwar

if there are no errors in form it successfully validates so i think i bind request data to form correctly

kirill-oficerov avatar Nov 06 '14 04:11 kirill-oficerov

Why if you call $form-> getErrors() instead ? Le 6 nov. 2014 05:48, "kirill-oficerov" [email protected] a écrit :

if there are no errors in form it successfully validates so i think i bind request data to form correctly

— Reply to this email directly or view it on GitHub https://github.com/Gregwar/CaptchaBundle/issues/111#issuecomment-61927194 .

Gregwar avatar Nov 06 '14 07:11 Gregwar

because $form->getErrors() most of the time returns an empty array if form is invalid ( http://stackoverflow.com/questions/11208992/symfony2-invalidform-without-errors ), so usually i use the way i showed in the first message, but this way does not show captcha errors

kirill-oficerov avatar Nov 27 '14 07:11 kirill-oficerov

Hm, ok However, $form->isValid() is false, right? The problem is that the error message is not present when you validate?

Gregwar avatar Nov 27 '14 08:11 Gregwar

However, $form->isValid() is false, right?

Yes

The problem is that the error message is not present when you validate?

Yes, it does not present in the array that is the result of the call: $this->get('validator')->validate($form);

kirill-oficerov avatar Nov 27 '14 08:11 kirill-oficerov