【问题标题】:Passing arguments from controller to form type __construct in Symfony 3在 Symfony 3 中从控制器传递参数以形成 __construct 类型
【发布时间】:2018-01-19 15:39:58
【问题描述】:

在 Symfony2 中我们使用:

$form = $this->createForm(new MyFormType($mySession));

我们可以在 __construct 中将 $session 作为参数传递。

但我无法在 Symfony3 中传递 $session 参数

我尝试过这样的事情:

$form = $this->createForm(MyFormType::class, array(
        'mySession' => $mySession
    ));

谁能指导我?如何解决这个问题。 提前致谢!

【问题讨论】:

    标签: php symfony symfony-2.1 symfony-3.3


    【解决方案1】:

    createForm 方法的第二个参数是您设置的 $datas。您可以使用 FormType configureOptions 中的 data_class 将数据链接到实体,如下所示:

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => MyEntity::class,
        ));
    }
    

    此外,Symfony 接受选项的第三个参数 以下是核心方法:

    /**
         * Creates and returns a Form instance from the type of the form.
         *
         * @param string $type    The fully qualified class name of the form type
         * @param mixed  $data    The initial data for the form
         * @param array  $options Options for the form
         *
         * @return Form
         */
        protected function createForm($type, $data = null, array $options = array())
        {
            return $this->container->get('form.factory')->create($type, $data, $options);
        }
    

    【讨论】:

      【解决方案2】:

      希望此链接对您有所帮助:

      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $this->yourVarableName = $options['yourVarableName']; // here you will catch your pass data
      
          $builder
              ->add('name', TextType::class)
              ...
              ->add('your_field_type', ChoiceType::class, array(
                  'label' => 'Label Field',
                  'mapped' => false,
                  'choices' => $this->traitChoices['figure_type']
              ))
              ...
          ;
      }
      
      /**
       * {@inheritdoc}
       */
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver->setDefaults(array(
              'data_class' => 'Foo\YouBundle\Entity\Goal',
              'yourVarableName' => null, // here your data
          ));
      }
      

      当你的控制器创建表单时:

      $goal = new Goal(); //instance of Entity
      $form = $this->createForm(GoalType::class, $goal, array(
              'action' => $this->generateUrl('profile_update'),
              'method' => 'PUT',
              'yourVarableName' => $yourVarableName,
          ));
      

      【讨论】:

      • 我们必须在哪里定义"$profile" 我对symfony3比较新,所以最好用一个例子来解释。谢谢
      • @mobizen 你明白吗?
      • 是的,我已经按照您告诉我的那样进行了更改...但是得到相同的错误“类型错误:函数 FD\PatientBundle\Form\GoalType::__construct(), 0 传递的参数太少”
      • @mobizen 您需要从 GoalType 中删除构造函数。出于所有意图和目的,S3 不再允许使用构造函数。 (好吧,理论上您可以将表单类型定义为服务,但这是另一回事)。
      猜你喜欢
      • 1970-01-01
      • 2017-10-12
      • 2014-09-04
      • 1970-01-01
      • 2022-08-04
      • 1970-01-01
      • 2019-05-16
      • 1970-01-01
      • 2016-12-15
      相关资源
      最近更新 更多