0

I am using the formbuilder to create a form as followed:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('content',    'textarea')
        ->add('rosters',    'entity', array( 
            'class'    =>   'PlatformBundle:team',
            'property' =>   'display',
            'multiple' =>   true,
            'expanded' =>   true,
            'required' =>   true
        ))
        ->add('send',       'submit')
    ;
}

At the moment I get all "teams". I need to adapt the form to display certain teams depending of the request. I can use the query-builder inside the form builder

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('content',    'textarea')
        ->add('rosters',    'entity', array( 
            'class'    =>   'PlatformBundle:team',
            'property' =>   'display',
            'query_builder' => function(TeamRepository $t) use ($userId) {
                return $r->createQueryBuilder('t')
                    ->where('(t.user = :user')
            },
            'multiple' =>   true,
            'expanded' =>   true,
            'required' =>   true
        ))
        ->add('send',       'submit')
    ;
}

But the query changes for different questionnaire. In brief: always the same questionnaire but different teams to be listed (Am I making sense?).

Does someone has an idea how dynamically modify the querybuilder inside a formbuilder?

2 Answers 2

1

I suggest two possible alternatives.

If the request comes from the form itself (i.e. you already submitted the form with some data and want to refine the fields) you can access the submitted data like this:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $data = $builder->getData();
    // now you can access form data

If the request comes from another source, you should use the "options" parameter. First, build a new $option for the requested user:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'user' => null,
    ));
}

Note: I set the default to null but you can set it to whatever you want.

After that you can pass the $option where you build the form, i.e.

// some controller
$option = array('user' => $request->get('user');
$teamForm = $this->createForm(new TeamType(), null, $options);
// ...
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks; that's another method; which seems lighter. worth a try! cheers
@Raphael_b happy to hear that!
0

For those looking for an answer...

The best solution I found is create a variable in the formtype and to import it form the controller. My formType will look like that:

class formType extends AbstractType
{
    // declare and construct the query in the class  to use it in the function
    private $qb; 
    public function __construct ($qb)
    {
        $this->qb = $qb;
    }
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // declare the variable within the function
        $qb = $this->qb;
        $builder
            ->add('content',    'textarea', array('required' => false))
            ->add('rosters',    'entity', array( 
                'class'    =>   'PlatformBundle:Team',
                // use ($qb) -> $qb is query built in the controller (or repository) 
                'query_builder' => function(TeamRepository $r) use ($qb) {
                    return $qb;
                },
                'property' =>   'display',
                'multiple' =>   true,
                'expanded' =>   true,
                'required' =>   true
            ))
            ->add('send',       'submit');
    }

In my controller I just pass $qb as an argument of the formtype

$qb = $this->getDoctrine()->getManager()->getRepository('PlatformBundle:Team')->qbteam($Id);

        $form = $this->createForm(new formType($qb), $form);

with qbteam a function in the team repository which return the query (not a result).

public function qbteam($Id){
    $qb = $this->createQueryBuilder('r')
        ->leftJoin('r.team', 'm')
        ->addSelect('m')
        ->where('m.user = :user')
        ->setParameter('user', $Id);
    return $qb; 
}

I hope it will help others. cheers

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.