使用javascript(Ajax)进行Symfony更新
在我的项目中,我的表中有一个select框,在select框值更改时,它应该更新数据库中的对象,我这样做,我得到select选项selected的值和应该更新的id,问题是数据库中的值没有修改, 我的控制器使用javascript(Ajax)进行Symfony更新,symfony,twig,Symfony,Twig,在我的项目中,我的表中有一个select框,在select框值更改时,它应该更新数据库中的对象,我这样做,我得到select选项selected的值和应该更新的id,问题是数据库中的值没有修改, 我的控制器 public function editAction(Request $request, Client $client) { $deleteForm = $this->createDeleteForm($client); $editForm = $this->cr
public function editAction(Request $request, Client $client)
{
$deleteForm = $this->createDeleteForm($client);
$editForm = $this->createForm('AppBundle\Form\ClientType', $client);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$status = $request->request->get('status');
$client->setStatus($status);
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('client_index');
}
return $this->render('client/edit.html.twig', array(
'client' => $client,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
在我的js中
function update(id, status){
var statusValue = {'status':status};
var path = Routing.generate('client_edit',{'id': id});
$.ajax({
type: "POST",
url: path,
data: statusValue
});
}
在客户机索引中的我的表中
<td>
<select id="status" name="status" onchange="update({{ client.id }}, this.value)">
<option {% if client.status is empty %}
selected
{% endif %}>Status</option>
<option {% if client.status == 'Envoyer') %}
selected
{% endif %}
value="Envoyer">Envoyer</option>
<option {% if client.status == 'Annuler') %}
selected
{% endif %}
value="Annuler">Annuler</option>
<option {% if client.status == 'Rejetter') %}
selected
{% endif %}
value="Rejeter">Rejeter</option>
</select>
地位
特使
环空器
雷杰特
所以在这里,我得到了id和select选项的值,但是我的数据库中没有任何更新,谁可以执行它来更新我的数据库的属性,谢谢 问题来自这一行
$editForm->isSubmitted() && $editForm->isValid()
因为你的表格从未提交过。
如果不需要表单验证,请将此行替换为:
//Check if it's an ajax request
if($request->isXmlHttpRequest()){
//you can get status directly with this
$request->get('status');
//Do some stuff...
->客户(美元);然后->刷新()?好的,谢谢,但没什么变化!那么您的代码是否通过了isValid检查?也就是说,您的表单是否包含一些可能导致表单/实体验证失败的其他字段(因为您没有向ajax请求传递任何其他字段)?我不知道,我只是传递了ajax中的状态!我如何解决验证失败的问题?