Zend framework - a hidden element does not matter even when specifying it

I create a hidden element this way:

$this->addElement('hidden', 'id', '1');

but I get the following:

<input type="hidden" name="id" value="" id="id" />

I also tried like this:

$this->addElement('hidden', 'id', array(
    'value' => 1
));

but it did not work better.

What's wrong?

+5
source share
8 answers

Maybe you are using

$form->populate($someData);

or

$form->isValid($someData);

somewhere in your code;)

+5
source

You can use the setValue method for Zend_Form.

Try the following:

$this->getElement('your-name')->setValue(1);
+1
source

, :

public function populate(array $values) {
    parent::populate($values);
    $this->addElement('hidden', 'hidden');
    $el = $this->getElement('hidden');
    $el->setValue(1);

}
+1

, $form->setValue(), param id - null.

0

(zf 1.1) int , ?:

$this->addElement( 'hidden', 'id', array('value'=>'1') )

0

:

//$form <- is your zend form element;
$form->get('element_name')->setValue(1);

!:)

As before, I said: make sure that there are no form configuration elements in your path (populate, setValues, etc.). :)

0
source

After the problem with this, I used (in the form class)

$hidden = $this->createElement('hidden','hiddenElement');
$hidden->setAttrib('xxx','my value');
$this->addElement($hidden);

Retrieve value with

$form->hiddenElement->getAttrib('xxx');

This is not an optimal solution, but it works for me.

0
source

Have you tried setDefault?

$this->addElement( 'hidden', 'id', array(
    'default' => 1
) );
-1
source

All Articles