Ami*_*uli 9 submit-button symfony-forms symfony
我想为具有许多属性的实体创建表单.为了确保数据输入的简便性,我想将该表单拆分为多个页面(例如2或3页).我们来看广告实体示例:
此拆分需要在移动到下一页之前在第一页中保存可用数据(插入数据库).不幸的是,由于限制,这是不可能的.
问题是:是否有任何文件或任何例子可以解决这个问题?
如果没有可用的文档,您认为最好将我的实体拆分为n个实体,以便每页有一个实体吗?
谢谢你的帮助.
Pic*_*oss 11
您可能应该使用CraueFormFlowBundle.它提供了构建多步骤表单的工具.
您可以为整个流创建一个表单类型,或者为每个步骤创建一个表单类型.
np8*_*p87 10
您不必拆分您的实体,而是分割您的表单:创建3个表单,每个表单包含广告实体所需的属性.
你需要:
在伪代码中,您的控制器将如下所示:
public function newAdStep1() {
new Ad() // New instance of $ad
new formStep1($ad) // The first form containing only the ad text field
// The form was filled, manage it...
form->isValid()? {
persist($ad); // Persist the first part of your ad object
forward(newAdStep2, $ad) // Go on to step 2, your $ad object as an argument
}
// ... or display step1 to user
createView createAdStep1.html.twig('form' => $form);
}
public function newAdStep2($ad) {
new formStep2($ad); // Now the second form, containing the "contact" fields
isValid ? {
persist($ad)
forward(newAdStep3, $ad)
}
createView createAdStep2($form, $ad); // Your $ad object needs to be sent to the view
}
public function newAdStep3($ad) {
new formStep3($ad); // Third and last form, containing the (X,Y) fields
isValid ? {
$em->persist($ad);
$em->flush(); // Your instance of $ad can be stored in database now
return('success !');
}
return view createAdStep3($form, $ad);
}
Run Code Online (Sandbox Code Playgroud)