2

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 works 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

2
  • 1
    My guess would be you are setting the value to empty at a later stage, possibly via the form isValid() method. Need to see more of the code to confirm. Commented Dec 9, 2010 at 23:48
  • Well, I was using 'idValid()' later on. Primarly I managed to find a workaround with 'HtmlTag' decorator, but then I decided to write my own Form class and things got way simpler. Thanks anyway Commented Dec 10, 2010 at 0:59

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

3
$hidden = new Zend_Form_Element_Hidden(array('name' => 'ratings', 'value' => 'ratings'));

Try it!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.