Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 - how to pass data to formBuilder?

I'm using entity choice list in my form. I want to use only specific entities (in example: only groups that user belongs to) So, in controller, I'm getting these groups, and trying to pass them into formBuider.

Controller:

/.../ $groups = $em->getRepository('VendorMyBundle:Group')->getUserGroups($user); $form = $this->createForm(new Message($groups), $message); /.../ 

so, what now? how to use it in formBuilder? how to change this line to use passed array of groups?

->add('group','entity',array('class' => 'Vendor\MyBundle\Entity\Group', 'label'=>'Group:')) 

or in the other way:

class MessageType { /.../   public function buildForm(FormBuilder $builder, array $options)   {     $builder       ->add('group','entity',         array(           'class' => 'Vendor\MyBundle\Entity\Group',           'property' => 'name',           'query_builder' => function ($repository) {             $qb = $repository->createQueryBuilder('group');             $qb->add('where', 'group.administrator = :user');             $qb->setParameter('user', $user->getId());             return $qb;           },           'label' => 'Group'         )       )       // Continue adding fields     ;   } /.../ } 

so how can i get object $user to use in form builder? ($user represent current logged user)

like image 428
jacobmaster Avatar asked Jul 16 '11 10:07

jacobmaster


2 Answers

You can give the object you want to use in the __construct() method.

Eg :

$form = $this     ->get('form.factory')     ->create(new ApplyStepOneFormType($this->company, $this->ad), $applicant); 

In your form type :

function __construct(\Your\Bundle\Entity\Company $company, \DYB\ConnectBundle\Entity\Ad $ad) {     $this->company = $company;     $this->ad = $ad; } 

And then in your form type in buildForm method :

$company = $this->company;     $builder->add('ad', 'entity', array(     'class' => '\Your\Bundle\Entity\Ad',     'query_builder' => function(\Your\Bundle\Repository\AdRepository $er) use ($company) {         return $er->getActiveAdsQueryBuilder($company);     }, )); 
like image 159
Bacteries Avatar answered Oct 01 '22 04:10

Bacteries


//In controller pass the value which you want to use in builder form in array like  $object = new Question(); $form->create(new QuestionType() , $object , array('sqtname'=>2,'question_type'=>2));   //In Form type class public function buildForm(FormBuilderInterface $builder , array $options)     {        //for setting data field dynamically    if (array_key_exists('question_type', $options) && $options['question_type'] != '') {     $data = $em->getReference("RecrutOnlineStandardBundle:StdQuestionType",$options['question_type']->getId());   } else {     $data = "";   }     $builder->add('StdQuestionType', 'entity', array(         'class' => 'TestStandardBundle:StdQuestionType',         'property' => 'name',         'empty_value' => 'Sélectionner un question type',         'required' => true,         'data' => $data,         'query_builder' => function(EntityRepository $er ) use ( $options ) {             if (isset($options['sqtname']) && $options['sqtname'] != '') {                 return $er->createQueryBuilder('sqt')                                 ->where("sqt.name!= ".$options['sqtname']);             } else{                return $er->createQueryBuilder('sqt');             }         }     ));  }   public function setDefaultOptions(OptionsResolverInterface $resolver)      {        $resolver->setDefaults(array(          'data_class' => 'Test\QuestionBundle\Entity\Question',          'required' => false,          'sqtname' => '',          'question_type' =>''         ));      } 
like image 23
user3335780 Avatar answered Oct 01 '22 03:10

user3335780