我是symfony和twig模板的新手,并且我正在使用使用Symfony中的formbuilder创建的表单提交数据的问题而苦苦挣扎。单击“创建”按钮不会提交数据。我创建了一个模板addEmployee.html.twig,它使用导航栏和侧边栏扩展了基本模板。
我注意到此表单仅在不扩展基础的情况下有效。因此,它将仅在创建表单的模板没有任何父模板的情况下创建适当的请求并提交表单。
我不知道为什么扩展base.html.twig模板会导致请求丢失,而且我不知道如何使其与已经构建的扩展模板一起使用。
MainController.php中的newEmployee函数
/**
* @Route("/employee/add", name="add_employee")
* @Method({"GET","POST"})
*/
public function newEmployee(Request $request){
$employee = new Employee();
$form = $this->createFormBuilder($employee)
->setMethod('POST')
->add('employeeNumber',TextType::class, array('attr' => array('class'=>'form-control')))
->add('name',TextType::class, array('attr' => array('class'=>'form-control')))
->add('phone',TextType::class, array('attr' => array('class'=>'form-control')))
->add('pool',TextType::class, array('attr' => array('class'=>'form-control')))
->add('save',SubmitType::class, array('label'=>'Create','attr'=>array('class'=> 'btn btn-primary mt-3')))
->getForm();
if ($request->isMethod('POST')) {
$form->submit($request->request->get($form->getName()));
}
if($form->isSubmitted() && $form->isValid()){
$employee=$form->getData();
$entityManager= $this->getDoctrine()->getManager();
$entityManager->persist($employee);
$entityManager->flush();
return $this->redirectToRoute('homepage');
}
return $this->render('/Employee/addEmployee.html.twig',array('form'=>$form->createView()));
}
addEmployee.html.twig'
{% extends 'base.html.twig' %}
{% block title%} New Article{% endblock %}
{% block body%}
{{form_start(form)}}
{{form_widget(form)}}
{{form_end(form)}}
{% endblock %}
base.html.twig
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}TL Manage{% endblock %}</title>
{% block stylesheets %}
<link rel="stylesheet" href="https://bootswatch.com/4/cosmo/bootstrap.min.css">
<link rel="stylesheet" href="../css/style.css">
{% endblock %}
</head>
<body>
{% include 'inc/navbar.html.twig' %}
{% include 'inc/sidebar.html.twig' %}
<div id="content">
{% block body %}{% endblock %}
</div>
{% block javascript %}
<!-- Icons.JS -->
<script src="https://unpkg.com/ionicons@4.5.5/dist/ionicons.js"></script>
<!-- JQUERY -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<!-- Popper.JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ" crossorigin="anonymous"></script>
<!-- Bootstrap JS -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script>
<!-- jQuery Custom Scroller CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/malihu-custom-scrollbar-plugin/3.1.5/jquery.mCustomScrollbar.concat.min.js"></script>
<script src="js/main.js"></script>
{% endblock %}
</body>
</html>
代替重定向到主页并插入数据 this is what happenes if I submit the form
,并从addEmployee.html.twig'表单中删除{% extends 'base.html.twig' %}
后,可以正常工作。有什么想法如何使它与其他ui部分一起工作吗?
答案 0 :(得分:0)
我自己找到了原因和解决方案,感到很尴尬...
事实证明,我在侧边栏中有另一种表单,该表单已包含在基本模板中,并且我没有关闭<form>
标签...所以就像这样:
inc / sidebar.html.twig
<form class="employee-Finder">
<input class="finder-input" type="text">
<form> <!-- this was the cause -->
因此第一个表单中的提交按钮可能是在侧边栏中提交了一个按钮,默认情况下它具有get方法。关闭标签后,一切正常。谢谢您的所有建议。