Issues

ZF-7404: Zend_Form_Element_Hash does not set a value when renderViewHelper() is called

Description

When you have a Zend_Form_Element_Hash element in your form, and you try to render the element not via the regular render() method, but via renderViewHelper(), then the element is rendered without the token set in the value.

The regular render() method is normally inherrited from Zend_Form_Element, but it is redefined so that the method initCsrfToken() is called to initialize the token in the session, before the rendering takes place.

Perhaps the renderViewHelper() method should be explicitly defined in Zend_Form_Element_Hash, so initCsrfToken() can also be invoked before rendering the ViewHelper.

Comments

I encountered this one for the second time. Would it be possible to have some kind of initialisation / preparation method that is called before the element is rendered?

As Tom points out, the initCsrfToken() method is only called from within the render() method.

Here is my proposed fix. I made the magic render method call the element's render() method, but the parent::render() call is skipped by checking a flag which is set before and after the render() call inside the magic method. Please review.

Sorry, was only checking out the resolve page, accidentally submitted ;)

Changed diffs to svn-generated diffs instead of Netbeans.

Patches applied to trunk and 1.9 release branch -- thanks!

The introduction of _isPartialRendering causes Zend_Form_Element_File::render() to be called from Zend_Form_Element_File::renderViewHelper(), which raises an exception when the form element has no decorators. This breaks the file upload form on our site. I've logged this as ZF-8419.

A better solution would be to implement Zend_Form_Element::partialRender(), with an empty default implementation, and for Zend_Form_Element_Hash to implement this to fix ZF-7404.