0
votes

I'm building a form allowing me to edit all the row of a given table at the same time with Symfony 1.4. To do so I've used embedded forms. My solution is working but I'm wondering why do I have to use an wrapper for my embedded forms?

Here is my code:

class FooCollectionForm extends BaseForm
{
  public function configure()
  {    
    $wrapperForm = new sfForm();
    foreach ($this->getOption('bar') as $bar)
    {
      $form = new myForm();
      $form->widgetSchema->setNameFormat('foo_collection['.$bar['label'] . '][%s]');
      $wrapperForm->embedForm($bar['label'], $form);
    }

    $this->embedForm('foo_collection', $wrapperForm);
  }
}

I find that the setNameFormat's parameter is quit ugly but it seems that it is the way it works. Do you agree on that or is there a better solution?

I'm building my form's layout manually and here is the loop I have to use to display all my fields:

<?php foreach ($form->getEmbeddedForm('foo_collection')->getEmbeddedForm() as $subForm): ?>
  <?php include_partial('form', array('form' => $subForm)) ?>
<?php endforeach; ?>

Again I find this ugly and I still don't understand why I can't do that $form->getEmbeddedForm() without using a wrapper form.

I tried to do the same form without the wrapper with a loop like this:

<?php foreach ($form->getEmbeddedForms() as $subForm): ?>

It is almost working except the fact my inputs have the same name and id.

1

1 Answers

1
votes

I find that the setNameFormat's parameter is quit ugly but it seems that it is the way it works. Do you agree on that or is there a better solution?

Yes, Symfony and embedded forms are a little bit confusing.

But yes - there is a better solution, you should let the form nameformat up to the framework.

Here's an excellent docu by fabien and ryan about the embedForm()/embedRelation() functions, the logic behind embedded form and also about rendering embedded forms.