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

时间:2017-10-25 10:53:47

标签: php forms symfony

我正在寻找答案,但我从来没有找到过什么。 。

我的首发位置: 包含此类/文件

控制器,DataDFu1Controller.php

表格 DataAPatientType.php,DataDFu1Type.php

实体 DataAPatient,DataDFu1

观看次数 / DataDfu1 / form.html.twig

DataDFu1Controller包含(概述)indexAction,newAction和 editAction等等。

两个Formtypes(DataAPatientType.php,DataDFu1Type.php)都有一个Form(look方法),这个表单稍后会在form.html.twig文件中为 newAction 和< EM> editAction

对于 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());

}

稍后将呈现表单。 。 。

首先我创建&#34; DataAPatientType.php&#34;表单,然后我添加&#34; DataDFu1Type.php&#34;形式。

在视图中 - &gt; form.html.twig它看起来像那样。

for DataDFu1Type:

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

for DataAPatientType:

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

所以我可以使用后缀&#39; dfu1&#39;来获得变量或函数。在表单之后。 一切都很好。我希望这种情况到现在为止是可以理解的..

现在我的问题:

我还必须创建一个 editAction ,其当然是相同的视图 - &gt; form.html.twig,包含数据集(实体)的填充值。在这个过程中,我不知道如何创建基于表单对象(DataAPatientType,DataDFu1Type)和相应的数据。 - &GT;我试图更具体

 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 
     *
     */
}

所以我只是不明白我如何基于两种类型(DataAPatientType.php,DataDFu1Type.php)创建一个Form,里面有相应的Data,我可以像在newAction中一样渲染它。

对于一个Form,我每次都这样做,并且它可以工作..但是对于两种类型我尝试了许多没有用的东西。有人有经验吗?或解决这个问题的方法?

form.html.twig的语法不可更改,因此必须使表单与newAction

中的表单相同

仅基于一个Type而不是两个

创建表单的示例
 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;
}

我真的希望,我的问题和问题可以理解

感谢 MJH

1 个答案:

答案 0 :(得分:0)

如果我理解你有这种形式:

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

然后在创建

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

在编辑中,您可以简单地执行类似

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

因此,如果您想设置一些默认值或覆盖现有值,您可以使用

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