passing data from controller to Type symfony2
Asked Answered
P

2

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.

Papilloma answered 18/10, 2011 at 12:43 Comment(1)
answered already... check #6717276Papilloma
E
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);
Embrocate answered 19/10, 2011 at 8:45 Comment(4)
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.phpPapilloma
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)Papilloma
:please post that answer again with the "use" statement so we can set this question as answered !!Papilloma
Passing a form type is now deprecated: #34028211Beaded
S
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.

Souter answered 18/10, 2011 at 14:4 Comment(8)
Can you elaborate more.. ? can you be more specific ??Papilloma
I edited my message to add more precisions on the getDefaultOptions() methodSouter
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 !!Papilloma
?!? Just do this : $plumeOptions['profile_id'] = 42... I think I don't get the real problem.Souter
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 ..."Papilloma
i have a table profile 1<->1 relationship with table plumeOptions. and table PlumeOptions 1<->1 relationship with table PhysicalPlume. and table Profile has a relationship of 1to200 PhysicalPlume. so in the table PhysicalPlume, there is a FK profile id. that's what i want my form to display: not all PhysicalPlumes, but only the ones related to that profile. Hope this helps a bit clarifying the picture..Papilloma
Please update your answer instead of putting code in comments.Souter
You get this error because $plumeOptions is supposed to be an array. Instead, you provide a PlumeOptions object. Too achieve your goald : should use a CollectionType type like this : sf.khepin.com/2011/08/… . Have a look to the newAction() method to see how objects are added to the collection.Souter

© 2022 - 2024 — McMap. All rights reserved.