2

I'm facing a problem with Symfony3 and forms.

I have a Parent form with an embedded Child form. From the controller, I can send data I can use in the Parent form with the $options array ($options['varA'], ...).

$form = $this->createForm(ParentEntityType::class, $objParent, array('varA'=>$varA, 'varB'=>$varB));

But what if I want to pass the varB variable (for example) to the embedded form ? What's the proper solution ?

Any help will be appreciated, thanks.

jeb
  • 78,592
  • 17
  • 171
  • 225
Heybee
  • 43
  • 1
  • 9

3 Answers3

2

Something like this, in the first form:

$builder->add('name', MyFormType::class, [
            'data' => $options['varB']
]);

But better if you share your forms codes. The main key is to pass variables by $options['key'] in buildForm() method.

Dr.X
  • 853
  • 9
  • 15
  • 2
    It's also possibly to use the 'entry_options' attribute, to pass any data to the embedded form. Like: `'entry_options' => [ 'varNameInEmbeddedForm' => $varB ];` That should be working since 2.8, I believe. – userfuser Oct 19 '19 at 00:39
0

this is a proper way in 2.7 in controller use this:

$itemform = $this->createForm(new SyllabusType(), $item, array('databranchid' => $branchid));

and in form use this:

    $builder
        ->add('studentclassid', 'entity', array(
            'class' =>'Schoolerp\Bundle\DBBundle\Entity\Studentclass',
            'choice_label' => 'name',
            'empty_value' => 'Choose an option',
            'query_builder'=>function(EntityRepository $e) use ( $options ){
                return $e->createQueryBuilder('u')->where('u.isactive=1')
                    ->andWhere('u.branchid = ?1')
                    ->setParameter(1, $options['databranchid']);
            }
        ))
        ->add('sectionid', 'entity', array(
            'class' =>'Schoolerp\Bundle\DBBundle\Entity\Sections',
            'choice_label' => 'name',
            'empty_value' => 'Choose an option',
            'query_builder'=>function(EntityRepository $e) use ( $options ){
                return $e->createQueryBuilder('u')->where('u.isactive=1')
                    ->andWhere('u.branchid = ?1')
                    ->setParameter(1, $options['databranchid']);
            }
        ));
/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Schoolerp\Bundle\DBBundle\Entity\Syllabus',
        'databranchid' => null
    ));
}

and if you use 3.0 replace input type into class type.

jkucharovic
  • 4,214
  • 1
  • 31
  • 46
0

it's just simple pass values this way:

 $builder->add('userid', 'entity', array(
            'class' =>'DBBundle\Entity\AdminUser',
            'choice_label' => 'name',
            'query_builder'=>function(EntityRepository $e) use ( $options ){
                return $e->createQueryBuilder('u')->where('u.status=1')
                    ->andWhere('u.id = ?1')
                    ->setParameter(1, $options['users']);
            },
            'attr' => array('style'=>'display:none'),
            'label_attr' => array('style' =>'display:none')));