ZF-8485: Zend_Form_Element_Captcha always fails if rendered piecemeal
Description
Related to [ZF-7404]. The reason my proposed fix was rather round-about was so that it would also cover the captcha form element. It still doesn't work, however. Before this, it isn't really possible to break apart and render separately a captcha element since important stuff is happening in {{render()}} which is skipped during {{__call()}}. Now it is possible to call {{renderCaptcha()}} and {{renderCaptcha_Word()}}, but they wouldn't ever match because the hidden element's value would be generated twice.
$this->_helper->viewRenderer()->setNoRender();
$form = new Zend_Form();
$form->addElement('captcha', 'captcha', array('captcha' => 'Dumb'))
->addElement('submit', 'submit');
if ($this->_request->isPost()) {
var_dump($form->isValid($this->_request->getPost()));
} else {
echo $form->renderForm(false);
echo $form->captcha->renderCaptcha();
echo '
';
echo $form->captcha->renderCaptcha_Word();
echo $form->submit;
echo '';
}
Action: enter the correct word Expected: true Actual: false
Unit test and proposed solution to follow.
Comments
Posted by Thomas Weidner (thomas) on 2009-12-07T00:16:12.000+0000
And where is the problem by calling...
??
Posted by Mon Zafra (monzee) on 2009-12-07T01:42:56.000+0000
An example is illustrated in the code above; i.e. if I wanted to insert arbitrary HTML between the captcha and the captcha input.
Posted by Mon Zafra (monzee) on 2009-12-07T04:09:18.000+0000
Attached patches to {{Zend_Form_Element}}, {{_Captcha}} and {{_Hash}}.
I added a pre-render hook to the base element class, which does nothing by default. What used to be in the {{render()}} methods of the Captcha and Hash classes are transferred into the hook. I also retconned my patch for [ZF-7404].
Posted by Mon Zafra (monzee) on 2009-12-07T04:09:50.000+0000
Attached unit tests