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.

1

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

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.

Comments

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')));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.