1

I have two entities, A and B. A has a one-to-many relation with B.

I am embedding five B forms inside a A form using the collection field type. To get that, I'm creating 5 A-B relations in my AController. What I would like to do is to use a field of each of this B entities to build its label in the form collection.

So, I have the following code:

//AController
$a = new A();

//Followinf returns an array of 5 B entities
$bs = $this->getDoctrine->getEntityManager()->getRepository('MyBundle:B')->findBy(array(
   'field' => 'value',
));

foreach ($bs as $b) {
   $a->addB($b);
}

$form = $this->createForm(new AType(), $a);

return array(
   'a' => $a,
   'form' => $form->createView(),
);

//AType
public function buildForm(FormBuilderInterface $builder, array $options)
{
   $builder
      ->add('a_field')
      ->add('another_field')
      ->add('bs', 'collection', array(
               'type' => new BType(),
               'options' => array(
                  'label' => 'i want to configure it depending on current B data',
                  )
               ))
      ;
}

I have found this related topic:

Symfony form - Access Entity inside child entry Type in a CollectionType

But notice this is different because it access the data in the child form. I want to access children data from the parent form and use it for each child label in the collection.

I know I can access child data using $builder->getData()->getBs(); but I can't figure how to use it later for each child form.

I know as well I can do it in the view, looping through the entities and using the loop index to render manually each collection element, but I would like to do it in the form.

Thanks a lot.

Community
  • 1
  • 1
Waiting for Dev...
  • 12,629
  • 5
  • 47
  • 57

1 Answers1

1

I think you want:

public function buildForm(FormBuilderInterface $builder, array $options)
{
   $data=someProcessingFunction($builder->getData()->getBs());
   $builder
      ->add('a_field')
      ->add('another_field')
      ->add('bs', 'collection', array(
               'type' => new BType(),
               'options' => array(
                  'label' => $data,
                  )
               ))
      ;
}

As a possibly related aside, you can add things onto the end in a separate $builder->add calls:

public function buildForm(FormBuilderInterface $builder, array $options)
{
   $builder->add('a_field');
   $builder->add('another_field');
   $data=someProcessingFunction($builder->getData()->getBs());
   $builder->add('bs', 'collection', array(
               'type' => new BType(),
               'options' => array(
                  'label' => $data,
                  )
               ))
      ;
}

If you are looking for label each one uniquely then this second approach is better as:

public function buildForm(FormBuilderInterface $builder, array $options)
{
   $builder->add('a_field');
   $builder->add('another_field');

   $data=someProcessingFunction($builder->getData()->getBs());
   foreach ($data as $k=>$v){
   $builder->add('b'.$k, null, array(
                  'label' => $v,
               ))
      ;
   }
}

or similar

Lighthart
  • 3,648
  • 6
  • 28
  • 47