Symfony 2 - how to pass data to formBuilder?

前端 未结 6 567
独厮守ぢ
独厮守ぢ 2020-11-30 03:50

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, a

相关标签:
6条回答
  • 2020-11-30 04:14

    If you want to use custom query, you have to set query_builder option as follows:

    use Doctrine\ORM\EntityRepository;
    
    ...
    
    $message = new Message();
    
    $form = $this->createFormBuilder($message)
                 ->add('group', 'entity', array(
                       'class' => 'Vendor\MyBundle\Entity\Group',
                       'label'=>'Group:',
                       'query_builder' => function(EntityRepository $er) {
                           return $er->createQueryBuilder('g')
                                     ->... // whatever you want to do
                           }
                        ))
                 ->getForm();
    

    You can find more info about query builder in Doctrine manual and about options for entity in Symfony2 manual.

    0 讨论(0)
  • 2020-11-30 04:15
    //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' =>'' 
           ));
         }
    
    0 讨论(0)
  • 2020-11-30 04:16

    Bacteries' solution IS NOT a good one. For example, if you declare your type as service, it is impossible to pass an object to constructor.

    A perfect solution is options - just pass data as option to form builder.

    0 讨论(0)
  • 2020-11-30 04:20

    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);
        },
    ));
    
    0 讨论(0)
  • 2020-11-30 04:22

    Bacteries' solution is a real good one. Just a note to save headache to other guy like me :)

    In this part may I point out the use ($company) part. It was hidden by the frame and of course nothing works properly without it.

    $builder->add('ad', 'entity', array(
       'class' => 
          '\Your\Bundle\Entity\Ad',
       'query_builder' => 
          function(\Your\Bundle\Repository\AdRepository $er) use ($company) {
                return $er->getActiveAdsQueryBuilder($company);
          },
        )
    );
    
    0 讨论(0)
  • Best way (my opinion) is give to your form entityManager and select all you need in it. But don't forget to declare empty key in setDefaults() otherwise data won't pass to your builder.

    Something like this one

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $options['em']->getRepository(''); // select all you need
        $builder->add('title', 'text')
                ->add('content', 'textarea');
    }
    
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Main\BlogBundle\Entity\Post',
            'validation_groups' => array('post'),
            'required' => false,
            'em' => null // this var is for your entityManager
            ));
    }
    

    Apply EM as simple option...

    0 讨论(0)
提交回复
热议问题