CaptchaBundle
CaptchaBundle copied to clipboard
incorrect captcha value - can't see error message if form is invalid
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;
}
Hello Do you correctly bind your form to the request?
if there are no errors in form it successfully validates so i think i bind request data to form correctly
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 .
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
Hm, ok
However, $form->isValid() is false, right?
The problem is that the error message is not present when you validate?
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);