How to handle Zend SubForms when the number of each is unknown-Collection of common programming errors
I have a ‘customer’ form which has a section called ‘contacts’. To start with this contacts section will contain the following elements..
But the user may want to add another contact which will duplicate the elements with javascript to produce the following:
I know how to produce the first set of elements, however if the form gets submitted and there are errors, how can i ensure that the correct number of ‘contacts’ elements get rendered?
-
Ive never had to do this with Zend_Form but i have done it with Symfony 1.4’s sfForm which has a similar API and theory of operation. Based on that the basic process is:
-
In the parent forms constructor intialize some default number of subforms. Youll want to separate out the logic for actually creating and embedding
n
subforms into a separate method(s). Ill refer to this as the methodemebedContacts($count = 1)
-
Override the
isValid
andsetDefaults
methods on the parent form so that they detect the number of subforms in the$data
arguments passed to them and then callembedContacts
before callingparent::isValid()
orparent::setDefaults()
.
Hope that helps.
-
Originally posted 2013-11-09 22:49:34.