Nicolas de Fontenay
Nicolas de Fontenay

Reputation: 2200

zend framework - hidden element has no value even when specifying it

I create a hidden element that way:

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

but what I get is this:

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

I've also tried like this:

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

but it didn't work better.

What's wrong?

Upvotes: 5

Views: 11501

Answers (8)

Hans Forsberg
Hans Forsberg

Reputation: 1

After problem with this I used (In form class)

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

Extract value with

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

It migth not be an optimal solution but it workked for me.

Upvotes: 0

Teodor Talov
Teodor Talov

Reputation: 1943

You can use the setValue method of Zend_Form.

Try it like this:

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

Upvotes: 1

Mr.TK
Mr.TK

Reputation: 1836

Answer is simple:

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

Worked for me! :)

As those before me said: make sure that there are no form elements setters in your way (populate, setValues, etc). :)

Upvotes: 0

Aigor
Aigor

Reputation: 11

I think you have to put in your form class:

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

}

Upvotes: 1

A.J. Brown
A.J. Brown

Reputation: 932

Have you tried setDefault?

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

Upvotes: -1

Tom&#225;š Fejfar
Tom&#225;š Fejfar

Reputation: 11217

You might be using

$form->populate($someData);

or

$form->isValid($someData);

somewhere in your code ;)

Upvotes: 5

AJJ
AJJ

Reputation: 7703

It works for me (zf 1.1) with an int or a string, but have you tried passing the value as string?:

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

Upvotes: 0

Iscander
Iscander

Reputation: 444

Perhaps before render you do $form->setValue() and param id is null.

Upvotes: 0

Related Questions