25

if i show a field of type "entity" in my form, and i want to filter this entity type based on a argument I pass from the controller, how do i do that.. ?

//PlumeOptionsType.php
public function buildForm(FormBuilder $builder, array $options)
{
    $builder->add('framePlume', 'entity', array(
        'class' => 'DessinPlumeBundle:PhysicalPlume',
        'query_builder' => function(EntityRepository $er) {
                                return $er->createQueryBuilder('pp')
                                    ->where("pp.profile = :profile")
                                    ->orderBy('pp.index', 'ASC')
                                    ->setParameter('profile', ????)
                                ;
                            },

    ));
}

public function getName()
{
    return 'plumeOptions';
}

public function getDefaultOptions(array $options)
{
    return array(
            'data_class'      => 'Dessin\PlumeBundle\Entity\PlumeOptions',
            'csrf_protection' => true,
            'csrf_field_name' => '_token',
            // a unique key to help generate the secret token
            'intention'       => 'plumeOptions_item',
    );
}
}

and inside the controller, i create the form :

i have that argument that i need to pass in my action code:
$profile_id = $this->getRequest()->getSession()->get('profile_id');
...
and then i create my form like this
$form = $this->createForm(new PlumeOptionsType(), $plumeOptions);

the $plumeOptions is just a class to persist. But it has a one-to-one relationship with another class called PhysicalPlume. Now, when i want to display the 'framePlume' in my code, i want to show a filtered PhysicalPlume entity.

1

2 Answers 2

40

You can pass parameters to the form class as follows:

//PlumeOptionsType.php
protected $profile;

public function __construct (Profile $profile)
{
    $this->profile = $profile;
}

Then use it in the query_builder of your buildForm:

$profile = $this->profile;

$builder->add('framePlume', 'entity', array(
    'class' => 'DessinPlumeBundle:PhysicalPlume',
    'query_builder' => function(EntityRepository $er) use ($profile) {
                            return $er->createQueryBuilder('pp')
                                ->where("pp.profile = :profile")
                                ->orderBy('pp.index', 'ASC')
                                ->setParameter('profile', $profile)
                            ;
                        },

));

And finally in your controller:

// fetch $profile from DB
$form = $this->createForm(new PlumeOptionsType($profile), $plumeOptions);
Sign up to request clarification or add additional context in comments.

4 Comments

thx for answering, I think you got exactly what i mean... Still, i got an error doing exactly what you suggested. Using $this when not in object context in PlumeBundle\Form\Type\PlumeOptionsType.php
pastebin.com/RVLFCxL4 pastebin.com/778ygFgR pastebin.com/q81k8w9A I think my problem is related to callback function. i can read the profile from inside PlumeOptionsType, but not from inside 'query_builder' => function(EntityRepository $er)
:please post that answer again with the "use" statement so we can set this question as answered !!
Passing a form type is now deprecated: stackoverflow.com/questions/34027711/…
4

You can use $plumeOptions to pass everything your argument, but you'll need to add a getDefaultOptions() in PlumeOptionsType to specify the default value for your option. See for instance https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/CheckboxType.php to see what this method should look like.

8 Comments

Can you elaborate more.. ? can you be more specific ??
I edited my message to add more precisions on the getDefaultOptions() method
Ok, let's say i added a default profile_id in the returned array of getDefaultOptions() of PlumeOptionsType. and i used ->setParameter('profile', $options['profile_id'])... but how to pass profile_id to the $plumeOptions at the first place?? Thank you for your help !!
?!? Just do this : $plumeOptions['profile_id'] = 42... I think I don't get the real problem.
i got this now: public function editPlumeOptionsAction(Request $request, $user_ou) { //get session and get profile_id from session $repository = $this->getDoctrine()->getRepository('DessinProfileBundle:Profile'); $profile = $repository->findOneById($profile_id); $plumeOptions = $profile->getPlumeOptions(); if(!$plumeOptions instanceof PlumeOptions) $plumeOptions = new PlumeOptions(); $plumeOptions['profile_id'] = $profile_id; $form = $this->createForm(new PlumeOptionsType(), $plumeOptions); i got an error "Cannot use object of type ..."
|

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.