简体   繁体   English

Symfony2基于两种不同类型创建填充表单(editAction)

[英]Symfony2 create filled Form based on two diffrent types (editAction)

I was looking for a answer, but i never found something. 我一直在寻找答案,但我从未发现任何东西。 .

My starting Position: This classes / files are included 我的出发位置:包含了这些类/文件

Controller , DataDFu1Controller.php 控制器 ,DataDFu1Controller.php

Form DataAPatientType.php ,DataDFu1Type.php 表格 DataAPatientType.php,DataDFu1Type.php

Entity DataAPatient, DataDFu1 实体 DataAPatient,DataDFu1

views / DataDfu1 / form.html.twig views / DataDfu1 / form.html.twig

The DataDFu1Controller contains (to the overview) the indexAction, newAction and editAction and so on. DataDFu1Controller包含(概述)indexAction,newAction和editAction等。

Both Formtypes (DataAPatientType.php ,DataDFu1Type.php) comes in one Form (look Method) this form goes to be rendered later in the form.html.twig file for the newAction and the editAction 两种Formtypes(DataAPatientType.php,DataDFu1Type.php)都采用一种Form(look方法),此表单稍后将在newActioneditAction的form.html.twig文件中呈现。

For the newAction i did it so: 对于newAction,我这样做是:

 private function createNewForm(DataAPatient $entity)
{
    $form = $this->createForm($this->get('data_livebundle.form.dataapatienttype'), $entity, array(
        'action' => $this->generateUrl('dataapatient_new'),
        'method' => 'POST',
    ));

    return $form->add('dFu1', new DataDFu1Type());

}

later the form comes rendered. 稍后,表单将呈现。 . .

So first i create "DataAPatientType.php" Form and then i add the "DataDFu1Type.php" to the form. 因此,首先我创建“ DataAPatientType.php”表单,然后将“ DataDFu1Type.php”添加到表单。

In the view -> form.html.twig it looks like that. 在视图-> form.html.twig中看起来像这样。

for DataDFu1Type: 对于DataDFu1Type:

{{ form_widget(form.dFu1.fu1Examiner1)}}

for DataAPatientType: 对于DataAPatientType:

{{ form_label(form.pSnnid, 'SNN-ID (if known)', {'label_attr':{'style':'margin-top:3px'}})}}

So i can get a variable or a function with the suffix 'dfu1' after the form . 因此,我可以在表单之后获取带有后缀'dfu1'的变量或函数。 Everything works so fine. 一切都很好。 I hope the condition are understandible till now.. 我希望这种情况到现在为止都是可以理解的。

Now my Problem: 现在我的问题是:

I have to create also an editAction which opend of course the same view-> form.html.twig with the filled values from a dataset (entity). 我还必须创建一个editAction ,它当然会使用来自数据集(实体)的填充值打开相同的view-> form.html.twig。 In this process i don't understand how i can create the Form Object based also (DataAPatientType, DataDFu1Type) with the corresponding data. 在此过程中,我不了解如何创建基于表单对象(DataAPatientType,DataDFu1Type)以及相应数据的表单对象。 -> I'm trying to be more specific ->我正在尝试更具体

 private function createEditForm(DataDFu1 $entity)
{    /*
     * This function shoud create the editform which insists 
     * DataAPatientType.php ,DataDFu1Type.php included the data from
     * $entity. I have the opportunity to get the entity for DataDFu1Type 
     * easy directly with the Primary Key and the data for DataAPatientType
     * over a Foreign Key which is safed in the $entity 
     *
     */
}

So i only dont understand how i can create a Form based on two types (DataAPatientType.php ,DataDFu1Type.php) with the corresponding Data inside, that i can render it like in the newAction. 因此,我仅不理解如何才能基于两种类型(DataAPatientType.php,DataDFu1Type.php)以及内部对应的数据来创建表单,我可以像在newAction中一样呈现它。

For one Form i did it everytime like so and it works.. but for two types i tried a lot things which didnt worked. 对于一种形式,我每次都这样做,并且可以工作..但是对于两种形式,我尝试了很多不起作用的事情。 Have somebody a experiance? 有经验吗? or a Solution for this Problem? 或此问题的解决方案?

the syntax of the form.html.twig isnt changeable so the form has to be rendered equivalent like in the newAction form.html.twig的语法不可更改,因此必须像newAction中一样将其呈现为等效形式

Example for creating a form based only on one Type and not two 仅基于一种类型而不基于两种类型创建表单的示例

 private function createEditForm(Event $entity)
{
    $form = $this->createForm($this->get('qcycle_eventbundle.form.eventtype'), $entity, array(
        'action' => $this->generateUrl('event_edit', array('id' => $entity->getId())),
        'method' => 'POST'
    ));
    $form->add('preview', 'button', array('label' => 'Preview', 'attr' => array('data-preview' => 'preview')))
        ->add('submit', 'submit', array('label' => 'Save Changes'))
        ->add('sendAndSave', 'submit', array('label' => 'Send Mail & Save'));

    return $form;
}

i really hope, that my problem and Question understandable 我真的希望我的问题和问题可以理解

thanks mjh 谢谢mjh

If i understand you have this form: 如果我了解您有以下表格:

class DataAPatientType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('dFu1', new DataDFu1Type());
        $builder->add('pSnnid', 'text');
        [...]
    }
}

Then in create 然后在创建

$form = $this->createForm(DataAPatientType(), new DataAPatient());

And in edit you can simply do something like 在编辑中,您只需执行类似

private function createEditForm(DataDFu1 $entity) 
{
   $form = $this->createForm(DataAPatientType(), entity); //here the form will be "populated" by the entity data

So if you want to set some default value or overwrite an existing value for example, you would use 因此,例如,如果您要设置一些默认值或覆盖现有值,则可以使用

private function createEditForm(DataDFu1 $entity) 
{
   entity->setPSnnid('whatever')
   $form = $this->createForm(DataAPatientType(), entity); //here the form will be "populated" by the entity data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM