2

I have the following code in my buildForm method of my FormType

$builder->add('privileges', 'entity', array(
                'label' => 'Privileges',
                'expanded' => true,
                'multiple' => true,
                'class' => 'AcmeStoreBundle:AdminPrivilege',
                'property'=> 'description',
                'query_builder' => function(EntityRepository $er) use ($category)
                {
                            return $er->createQueryBuilder('p')
                                    ->where('p.categoryid = :categoryID')
                                    ->andWhere('p.parentid = -1')
                                    ->setParameter('categoryID', $category->getId())
                                    ->orderBy('p.position', 'ASC');
                }
            ));

Here if the parentid is greater than -1, then i'd like to show further form components after the checkbox where parentid is greater than -1 is created.

I've searched over Google and have been unable to find a way to do this, can anybody help?

1 Answer 1

1

Mat. If I understand correctly, you can inject parentid and entity manager to form type construct from controller. So you can run query before add field to builder, and use if-else. For example:

public function __construct($parentId, $em)
{
    $this->parentId = $parentId;
    $this->em = $em;
}

public function buildForm(FormBuilder $builder, array $options)
{
    $choices = $this->em->getRepository()->callNeededMethod();

    if($this->parentId){
        $builder->add([someFieldParams]);
    }else{
        $builder->add([anoutherFieldParams]);
    }
}
Sign up to request clarification or add additional context in comments.

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.