Symfony2 - How to set, and get, options when using a Form Class?

后端 未结 6 1464
暖寄归人
暖寄归人 2021-02-06 08:29

I am using Form Classes to build the various form in my project.

In the Entity Type file, for the buildForm function, there is a secondary parameter of \"array $options\

6条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-06 09:02

    Let me show you what worked for me

    In the Controller:

    $form = $this->createForm(new UsersType(), $entity, array(
        'attr' => array('locationId' => $currentLocationId)));
    

    In the FormType:

    ->add('location', 'entity', array(
            'class' => 'Ro\RoinventBundle\Entity\Locations',
             'query_builder' => function (\Doctrine\ORM\EntityRepository $er) use ($options)
            {
                if (isset($options['attr']['locationId']) && ($options['attr']['locationId'] != NULL))
                {
                    return $er->createQueryBuilder('Locations')
                        ->where('Locations.id = :param')
                        ->setParameter('param', $options['attr']['locationId']);
                }
                //else do what you want
    },
    ));
    

提交回复
热议问题