views:

85

answers:

1

Hi

I am using the Zend Framework and have setup a normal Zend Form, what I want to try to achieve is to have a button (with some javascript) that says add more and it adds another drop down menu same as the one setup in the zend form (a clone of it).

basically when the button is clicked it adds another select box like so:

<select name="type[]"> ...</select>

I can do a copy of the multi select box with a different name and insert it in the DOM and catch the post from the controller outside of the Zend form but what I was wondering if there is a proper way to achieve this and be able to validate and populate the extra fields when editing a current data stored in db if there are any extra.. Any help is appreciated, thanks.

+2  A: 

Well remember that in your controller if you have something like:

$this->form = new Form_Someform();

You can always do:

$this->form->addElement(etc...)

Right before using isValid() or populate.

So in your controller when someone submits the form, when creating your form object you could check if any select were created dynamicaly and then create corresponding Zend_Elements and just validate against that. Also when you reload that form you just create elements depending on whats in your database.

You could also use the forms constructor to pass in an array of selectboxes and create then right there too. Thats what I do.

The important things to remember is that you have control on the constructor and on the form object between its creation and the use of the populate() and isValid() functions.

Hope this helps.

Iznogood
Yes, actually I came to the same conclusion. I created them dynamically as needed inside the controllerThanks for your reply Iznogood!
silverphp
@silverphp hey thank YOU for accepting my answer.
Iznogood
:) no probs, I think that is the correct way to do it.
silverphp