Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing data from controller to Type symfony2

Tags:

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.

like image 960
xeon Avatar asked Oct 18 '11 12:10

xeon


2 Answers

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); 
like image 62
Lou Terrailloune Avatar answered Sep 19 '22 11:09

Lou Terrailloune


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.

like image 32
greg0ire Avatar answered Sep 21 '22 11:09

greg0ire