0

I have a 'customer' form which has a section called 'contacts'. To start with this contacts section will contain the following elements..

<input type="text" name="contacts[0][fname]" />
<input type="text" name="contacts[0][sname]" />

But the user may want to add another contact which will duplicate the elements with javascript to produce the following:

<input type="text" name="contacts[0][fname]" />
<input type="text" name="contacts[0][sname]" />
<br />
<input type="text" name="contacts[1][fname]" />
<input type="text" name="contacts[1][sname]" />

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?

spooky
  • 421
  • 5
  • 20
  • There's a similar question here: http://stackoverflow.com/questions/6831145/zend-form-dynamic-adding-subforms , it could be helpful. – dinopmi Feb 15 '12 at 15:26
  • Turns out this is a bug in ZF that is not fixed until v2.0 - http://stackoverflow.com/questions/2849884/howto-address-specific-element-from-subform-and-have-it-displayed-correctly-with – spooky Feb 17 '12 at 16:04

1 Answers1

0

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:

  1. 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 method emebedContacts($count = 1)

  2. Override the isValid and setDefaults methods on the parent form so that they detect the number of subforms in the $data arguments passed to them and then call embedContacts before calling parent::isValid() or parent::setDefaults().

Hope that helps.

prodigitalson
  • 60,050
  • 10
  • 100
  • 114
  • 1
    Hi, thanks, that has helped. I was confused however about how to go about grouping them under the 'contacts' element, but what i ended up doing was creating one empty subform, assigning that as 'contacts' which in turn contained the indexed subforms, and which then in turn contained the fname, sname etc elements. – spooky Feb 16 '12 at 12:47
  • Sorry, still having problems - what i've done above sets up the form correctly, however when i try to render the subform elements directly, their names do not appear in array format i.e. they appear as name="fname" rather than name="contacts[0][fname]" – spooky Feb 16 '12 at 16:24
  • There is a methos you may need to call manually or pass an option in... i think its something like `Zend_Form::setIsArray` but im not sure youll have to check the docs. Also make sure your passing the second argument to `addSubform` which is the name/key you will later access it with and what will be encode in the `name` attribute of elements. – prodigitalson Feb 16 '12 at 16:39
  • Hi, thanks, but i've done all that - the elements render fine if i echo the $form out, but when i echo the subform elements directly the names do not get formatted correctly – spooky Feb 16 '12 at 17:08