4

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, actually I came to the same conclusion. I created them dynamically as needed inside the controller Thanks for your reply Iznogood!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.