andreea115
andreea115

Reputation: 299

zendframework form2 how to set a value in a hidden form element l

quick question.

i am trying to set a value in a hidden form element. this is what i did below;but its not working.

i am trying to add the value 7 to the hidden form field. i used the value options field. but its not working.

    $this->add(array(
        'type' => 'Zend\Form\Element\Hidden',
        'name' => 'id',
        'options' => array(
            'value_options' => array(
                'id'=> 7 ,
      ),  ),

    ));

below is my form page:

namespace Workers\Form\Fieldset;


use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;

class JobSortFieldset extends Fieldset implements InputFilterProviderInterface
{
    public function __construct(ObjectManager $objectManager, $id   )
    {
        parent::__construct('JobSort');



        $this->setHydrator(new DoctrineHydrator($objectManager, 'Workers\Entity\JobSort'))
             ->setObject(new JobSort());


        $this->add(array(
            'type' => 'Zend\Form\Element\Hidden',
            'name' => 'id',
            'options' => array(
                'value_options' => array(
                    'id'=> 7 ,
          ),  ),

        ));

Upvotes: 0

Views: 1277

Answers (1)

lku
lku

Reputation: 1742

Option value_options is used for multivalue elements (MultiCheckbox, Select, etc.), for simple element like Hidden just set the value attribute:

$this->add(array(
    'type' => 'Zend\Form\Element\Hidden',
    'name' => 'id',
    'attributes' => array(
        'value' => 7,
    ),
));

Upvotes: 3

Related Questions