[英]Symfony2 - Sending other data along with form data via Ajax causes IsValid() to return false
I'm trying to send other data along with a submitted form to a controller inside Symfony2. 我正在尝试将其他数据以及提交的表单发送到Symfony2中的控制器。
When I try this like: 当我这样尝试时:
$("#submit_btn").on("click", function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: post_url,
data: form.serialize()
});
});
I see i got a successful POST
request followed by a redirect as intended inside the controller action, If IsValid()
returned true. 我看到我收到了一个成功的
POST
请求,然后在控制器操作中按预期进行了重定向,如果IsValid()
返回true。
But when I try to send other data with the form like: 但是,当我尝试使用以下形式发送其他数据时:
$("#submit_btn").on("click", function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: post_url,
data: { form: form.serialize(), otherdata: "test" }
});
});
I do not get the redirection 302
response. 我没有收到重定向
302
响应。 instead I get only one 200
response when means IsValid()
method returned false. 相反,当
IsValid()
方法返回false时,我只收到一个200
响应。 My question here how to not only send form, but also other data with it ? 我的问题在这里不仅要发送表单,还要发送其他数据吗?
Here's my controller action: 这是我的控制器动作:
public function postOverviewAction(Request $request, $id)
{
$overview = $this->get("doctrine_mongodb")->getRepository("GbrBEBundle:Overview")->findOneById($id);
$overview_photos = $this->get("doctrine_mongodb")->getRepository("GbrBEBundle:OverviewPhoto")->findAll();
$form = $this->createForm(new OverviewType(), $overview);
$form->handleRequest($request);
$height = $form->get("coordinate_height")->getData();
$width = $form->get("coordinate_width")->getData();
$x = $form->get("coordinate_x")->getData();
$y = $form->get("coordinate_y")->getData();
if($form->isValid())
{
$overview->setCropCoordinates(array('height' => $height, 'width' => $width, 'x' => $x, 'y' => $y));
$dm = $this->get("doctrine_mongodb")->getManager();
$dm->persist($overview);
$dm->flush();
return $this->redirect($this->generateUrl("gbr_be_get_overview"));
}
return $this->render("GbrBEBundle:Default:overview.html.twig", array(
"form" => $form->createView(),
"overview" => $overview,
"overview_photos" => $overview_photos,
));
}
You can add an unmapped field to the form: 您可以向表单添加未映射的字段:
How do I add an unbound field to a form in Symfony which is otherwise bound to an entity? 如何在未绑定到实体的Symfony表单中添加未绑定字段?
Setting to type text
should prove the most versatile. 设置键入
text
应该证明是最通用的。
You can also create a collection
field as the unmapped field, and assign the text
type to it. 您还可以创建一个
collection
字段作为未映射的字段,并为其指定text
类型。 This will allow you to have multiple additional text data on the receiving end. 这将使您在接收端拥有多个其他文本数据。
http://symfony.com/doc/current/reference/forms/types/collection.html http://symfony.com/doc/current/reference/forms/types/collection.html
may be try to change: 可能尝试更改:
data: { form: form.serialize(), otherdata: "test" }
by data: form.serialize()+'&otherdata=test', 按数据:form.serialize()+'&otherdata = test',
i think its because .serialize() return a string. 我认为是因为.serialize()返回一个字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.