参数传递,形成的Symfony 3

问题描述:

型__construct在Symfony2中,我们使用:参数传递,形成的Symfony 3

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

我们可以通过$会议在__construct的参数。

但我不能够通过$会话参数在Symfony3

我已经试过这样的事情:

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

任何人都可以请指导我?如何解决这个问题。 在此先感谢!

createForm方法的第二个参数是您设置的$数据。您可以将数据使用data_class这样的链接到一个实体在FormType configureOptions:

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); 
    } 

我希望这个链接帮助您:

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, 
    )); 
+0

我们必须定义“$ profile” 我更喜欢symfony3,所以最好用一个例子来解释。 谢谢 – mobizen

+0

@mobizen你不知道? –

+0

是的,我已经做出了改变,因为你告诉我... 但得到相同的错误“类型错误:太少参数函数FD \ PatientBundle \ Form \ GoalType :: __构造(),0通过” – mobizen