基本上这段代码打印为true:
/**
* Deletes myentity.
*
* @Route("/{id}", name="myentity_delete")
* @Method("DELETE")
*/
public function deleteAction(Request $request, MyEntity $entity) {
$form = $this->createDeleteForm($entity);
$form->handleRequest($request);
var_dump($form->isSubmitted());
die;
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->remove($invitation);
$em->flush();
}
return $this->redirectToRoute('homepage');
}
/**
* Creates a form to delete a myentity.
*
* @param MyEntity $entity The entity
* @return Form The form
*/
private function createDeleteForm(MyEntity $entity) {
return $this->createFormBuilder($entity)
->setMethod('DELETE')
->getForm();
}
但是如果我更改createDeleteForm()方法以禁用CSRF保护,则打印为false(isSubmitted()返回false):
private function createDeleteForm(MyEntity $entity) {
return $this->createFormBuilder($entity, array('csrf_protection' => false))
->setMethod('DELETE')
->getForm();
}
我需要这样做,因为我想发送一封内嵌嵌入删除表单的电子邮件。
编辑1: 即使表单显示在网站上,它仍然无效,因此错误与电子邮件无关。
答案 0 :(得分:0)
所以我终于明白了...... 表单未提交,因为它不包含任何字段。我调试了$ request-> request-> all()方法来检查它的内容,我用CRSF得到了这个:
array(2) {
["_method"]=>
string(6) "DELETE"
["form"]=>
array(1) {
["_token"]=>
string(43) "ENi9OFbTVfkJO8IFzahXUGOXe79tamHy3m04vgd7ZbM"
}
}
没有它:
array(1) {
["_method"]=>
string(6) "DELETE"
}
然后在文件HttpFoundationRequestHandler.php中,以下代码不返回任何内容(变量$ name等于“form”)。
if ('' === $name) {
$params = $request->request->all();
$files = $request->files->all();
} elseif ($request->request->has($name) || $request->files->has($name)) {
$default = $form->getConfig()->getCompound() ? array() : null;
$params = $request->request->get($name, $default);
$files = $request->files->get($name, $default);
} else {
// Don't submit the form if it is not present in the request
return;
}