[英]Symfony2 create filled Form based on two diffrent types (editAction)
我一直在尋找答案,但我從未發現任何東西。 。
我的出發位置:包含了這些類/文件
控制器 ,DataDFu1Controller.php
表格 DataAPatientType.php,DataDFu1Type.php
實體 DataAPatient,DataDFu1
views / DataDfu1 / form.html.twig
DataDFu1Controller包含(概述)indexAction,newAction和editAction等。
兩種Formtypes(DataAPatientType.php,DataDFu1Type.php)都采用一種Form(look方法),此表單稍后將在newAction和editAction的form.html.twig文件中呈現。
對於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());
}
稍后,表單將呈現。 。 。
因此,首先我創建“ DataAPatientType.php”表單,然后將“ DataDFu1Type.php”添加到表單。
在視圖-> form.html.twig中看起來像這樣。
對於DataDFu1Type:
{{ form_widget(form.dFu1.fu1Examiner1)}}
對於DataAPatientType:
{{ form_label(form.pSnnid, 'SNN-ID (if known)', {'label_attr':{'style':'margin-top:3px'}})}}
因此,我可以在表單之后獲取帶有后綴'dfu1'的變量或函數。 一切都很好。 我希望這種情況到現在為止都是可以理解的。
現在我的問題是:
我還必須創建一個editAction ,它當然會使用來自數據集(實體)的填充值打開相同的view-> form.html.twig。 在此過程中,我不了解如何創建基於表單對象(DataAPatientType,DataDFu1Type)以及相應數據的表單對象。 ->我正在嘗試更具體
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)以及內部對應的數據來創建表單,我可以像在newAction中一樣呈現它。
對於一種形式,我每次都這樣做,並且可以工作..但是對於兩種形式,我嘗試了很多不起作用的事情。 有經驗嗎? 或此問題的解決方案?
form.html.twig的語法不可更改,因此必須像newAction中一樣將其呈現為等效形式
僅基於一種類型而不基於兩種類型創建表單的示例
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
如果我了解您有以下表格:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.