Passing arguments from controller into zend form

This is a bit of a strange one.  Trying to pass in generic variables into a zend form can be tricky, but I got an answer to this today and no doubt I will need to remember this one so here it is.

I always create my forms in a separate file and instantiate them in the controllers.  I used to create the whole form in the init() method of the form class, but in order to pass variables into this class you must pass them in first and then create the elements you wish to use the variable with etc.  So for example my form might have 3 methods:

class Default_Form_Someform extends Zend_Form
{
//declare a private variable
private $my_var;

public function init()
{

}

public function setmyvar($v_id)
{
//set the variable
$this->my_var = $v_id;
}

public function startform()
{
$this->setMethod('post') ->setAttrib('enctype', 'multipart/form-data');
$this->setName('someform');
$this->setAction($_SERVER["REQUEST_URI"]);

$this->addElement('Text', 'somefield', array(
'Label' => 'Text:',
'required' => true
))
->getElement('somefield')
->setAttrib('width', '500');

//show var here
var_dump($this->my_var);

$this->addElement('submit', 'Save')
->getElement('Save')
->setLabel('Save')
->setAttrib('value', "Save and Finish");
}
}

Then once I have a form in this setup I can simply set the value of $this->myvar in the controller and you should see the variable dumped onto the screen to do whatever you wish with.  So my controller action might look like this:

public funcion someAction()
{

//create instance of form
$form = new Default_Form_Someform();
//set value of object variable
$form->setmyvar('some value is put in here');
//create the actual form elements using you objects variable you set.
$form->startform();

//pass this to the view
$this->view->form = $form;

}

So basically I have simply stopped the my form from finishing its business before I have set my variable to a value.  Only once I have set it I then create the actual elements needed.

Hope this helps as I couldn’t really find much on the web about this.

Tags: , ,

11 Responses to “Passing arguments from controller into zend form”

  1. James says:

    Worked like a charm, thanks a million! Just a note for anyone else, if you’re using setValue() in for form elements in your controller, be sure to place it after your $form->startForm() statement!

  2. Thank you for your post!
    You’ve saved me a load of time, works great

  3. Mike Pearce says:

    Thanks for this, pretty useful.

    One thing though, Zend_Form uses __toString() on the form object, so, if you wanted to do

    print $this->startForm();

    You could always just add:

    return $this;

    To the end of the startForm() function. ;)

  4. Anon says:

    I prefer to store the form elements I wish to set values for as private variables:

    class Default_Form_Someform extends Zend_Form
    {
    private $idElement;

    public function setId($id)
    {
    $this->idElement->setValue($id);
    }

    public function init()
    {
    $this->idElement = new Zend_Form_Element_Hidden(‘id’);
    }
    }

    Then you can create the form and set the values after the form is alive by manipulating it. As long as you do this before it’s rendered it seems to work fine.

    Not sure of any drawbacks to this method… comments welcome..

  5. Zensys says:

    What I do to pass variables to my form is passing them to the constructor. In the constructor you first call the parent::constructor and then initiate your form as in init():

    [code]
    class Default_Form_Someform extends Zend_Form
    {

    public function __construct($my_var)
    {
    parent::__construct();

    $this->setMethod('post') ->setAttrib('enctype', 'multipart/form-data');
    $this->setName('someform');
    $this->setAction($_SERVER["REQUEST_URI"]);

    $this->addElement('Text', 'somefield', array(
    'Label' => 'Text:',
    'required' => true
    ))
    ->getElement('somefield')
    ->setAttrib('width', '500');

    //show var here
    var_dump($my_var);

    $this->addElement('submit', 'Save')
    ->getElement('Save')
    ->setLabel('Save')
    ->setAttrib('value', "Save and Finish");
    }
    }
    [/code]

    Keeps your form simpler, closer to standard and in your controller you just need to instantiate your form:

    [code]$form = new Application_Form_SomeForm($myVar);[/code]

  6. Maarten says:

    Thanks for sharing this one. I keep telling myself to write these tips down as well, but never get myself to actually do it.

    Cool effect btw on the background.

  7. [...] Passing arguments from controller into zend form (apr 2010) [...]

  8. dennis_dc says:

    thanks for this, works very well.

  9. adrian says:

    Its possible to set the values after the form is available.
    http://framework.zend.com/manual/en/zend.form.forms.html
    [quote]
    Sometimes you’ll want to populate the form with specified values prior to rendering. This can be done with either the setDefaults() or populate() methods:

    1.$form->setDefaults($data);
    2.$form->populate($data);
    [/quote]

  10. Thanks for this. Very helpful. Cheers bud :)

  11. Aleksandar says:

    Well I’ll use:

    $form->getElement(‘my_var’)->setValue($some_value);

    inside my controller.

    Cheers

Leave a Reply