我正在参与Symfony项目,所以这就是我的问题:
我有一个实体,对于该实体,我在同一页面中有两个表单(一个用于插入,另一个用于更新插入的)。 所以我做到了。
ActionMethod
public function adminTareasAction(Request $request) {
$newTareaForm = $this->createForm(TareaType::class, null, array("formType" => "newTarea"));
$editTareasForm = $this->createForm(TareaType::class, null, array("formType" => "editTareas"));
if($request->isMethod("POST")) {
if(!is_null($request->request->get('newTarea'))) {
$newTareaForm->handleRequest($request);
if($newTareaForm->isSubmitted() && $newTareaForm->isValid()) {
$newTarea = $newTareaForm->getData();
$dataManager = $this->getDoctrine()->getManager();
$dataManager->persist($newTarea);
$dataManager->flush();
return $this->redirectToRoute("admin_tareas");
}
}
elseif(!is_null($request->request->get('editTareas'))) {
$editTareasForm->handleRequest($request);
if($editTareasForm->isSubmitted() && $editTareasForm->isValid()) {
$newTarea = $newTareaForm->getData();
$dataManager = $this->getDoctrine()->getManager();
$dataManager->persist($newTarea);
$dataManager->flush();
return $this->redirectToRoute("admin_tareas");
}
}
}
$tareas = $this->getDoctrine()->getRepository('FabricacionBundle:Tarea')->findAll();
if(!$tareas) {
$tareas = "No hay Tareas";
}
return $this->render('UsersBundle:Admin:adminTareas.html.twig', array("newTareaForm" => $newTareaForm->createView(), "editTareasForm" => $editTareasForm->createView(), "tareas" => $tareas));
}
类型类
class TareaType extends AbstractType {
private $formType;
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$this->formType = $options["formType"];
if($this->formType == "newTarea") {
//var_dump();
$builder
->add('tareaName', TextType::class)
->add('tareaOrden', IntegerType::class)
->add('submitNewTarea', SubmitType::class);
}
elseif($this->formType == "editTareas") {
$builder
->add('newName', CollectionType::class, array("entry_type" => TextType::class, "allow_add" => true))
->add('newOrden', CollectionType::class, array("entry_type" => IntegerType::class, "allow_add" => true))
->add('deleteTarea', CollectionType::class, array("entry_type" => CheckboxType::class, "allow_add" => true))
->add('submitTarea', SubmitType::class);
}
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(array(
'data_class' => 'FabricacionBundle\Entity\Tarea',
"formType" => null
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix() {
/*if($this->formType == "newTarea") {
return $this->formType;
}
elseif($this->formType == "editTareas") {
return $this->formType;
}*/
return 'FabricacionBundle_tarea';
}
}
我唯一需要的是更改表单名称,然后按名称在控制器中处理它们。
答案 0 :(得分:0)
只需删除getBlockPrefix()
。您正在通过其FQCN访问您的字段类型:
$newTareaForm = $this->createForm(TareaType::class, null, array("formType" => "newTarea"));
所以你不需要为它设一个别名(不推荐使用)。
答案 1 :(得分:0)
好吧,阅读Form类我得到了我必须做的事。
就这样创建表单:
$newTareaForm = $this->container->get('form.factory')->createNamedBuilder("newTarea", TareaType::class, null, array("formType" => "newTarea"))->getForm();
$editTareasForm = $this->container->get('form.factory')->createNamedBuilder("editTareas", TareaType::class, null, array("formType" => "editTareas"))->getForm();