I’ve been using Symfony’s form framework for a while now. But would like to know if anyone has a better approach to embedding forms dynamically.
The problem arises when I embed a form (see bottom), I need to give it an array index, as Fabian explains how the sfForm object is like a multi-dimensional array in this article Advanced forms.
If I want to give the user the ability to click a button and embed another form, how can I achieve the following if they click the button multiple times:
<input type="parent[child][]" />
<input type="parent[child][]" />
<input type="parent[child][]" />
… repeated how many time user clicks a button. I can use fast javascript to copy and paste form elements in DOM.
Rather than this:
<input type="parent[child][1]" />
<input type="parent[child][2]" />
<input type="parent[child][3]" />
… repeated how many times user clicks a button. Requires javascript method to count how many times user clicks the button, ie to set correct array index. Also requires Ajax to call a PHP function that embeds form with this array index. I would like to avoid using this method if possible.
How I embed a form:
$parentForm = new ParentForm($parent)
$child = new child();
$child->setParent($parent);
$sfForm = new sfForm();
$sfForm ->embedForm($someIndex, new ChildForm($child));
$parentForm->embedForm('child', $sfForm);
Hey, I found a way to do! The tricky part here is to override sfWidgetFormSchema::generateName method.
Now, you only need to set this to your ‘wrapper’ form. Here’s my example with “Master has many Slaves” schema:
Notice the ‘slaves_count’ option that I pass from executeCreate like this:
Now you can easily use jQuery to clone rows and not bother about index! Cheers.