Zend-form setValue , view has empty value like <input value="" >
I have a quite complex form setup using Zend-Form. At one point I'm setting value of a hidden input using :
$oHidden = new Zend_Form_Element_Hidden('ratings'.$k);
$oHidden->setValue('ratings');Zend_Debug::dump($oHidden);
$this->addElements(array($oHidden));
This method wor开发者_开发百科ks well in other places of the same form, but this one, and another one just like i t outputs :
<input type="hidden" name="ratings1" value="" id="ratings1" />
I've dumped the $oHidden variable and it outputs :
object(Zend_Form_Element_Hidden)#143 (29) {
...
["_value":protected] => string(7) "ratings"
["_view":protected] => NULL
["_isPartialRendering":protected] => bool(false)
}
So it sets the value just fine for a while but it doesn't render it. Please let me know where to start looking reasons for this behavior.
Thanks, Alek
The problem is precisely the isValid()
function. It clears all values from the form and then repopulates it with the parametres that are passed to it. If a parametre is absent, it apparently won't appear anymore in the form, even if it was set explicitly a few lines earlier.
My case was an optional "redirect" hidden field in a login form. Here's the code (simplified for readability):
$form = new Form_Login();
$redirect = $this->_getParam('redirect','/user/login/welcome');
$form->addElement('Hidden','redirect',array('value' => $redirect));
if ($this->_request->isPost() && $form->isValid($this->_getAllParams())) {
// WTF! the "request" field has no value!!!
}
The workaround was setting the action parametre:
$form = new Form_Login();
$redirect = $this->_getParam('redirect','/user/login/welcome');
$this->_setParam('redirect',$redirect);
$form->addElement('Hidden','redirect',array('value' => $redirect));
if ($this->_request->isPost() && $form->isValid($this->_getAllParams())) {
// AHA! now it works!
}
I know the question is half a year old, but oh well, better late than never :D.
$hidden = new Zend_Form_Element_Hidden(array('name' => 'ratings', 'value' => 'ratings'));
Try it!
精彩评论