大家好我在symfony2.7工作,我的问题是当我想要检查复选框时,我希望没有重定向的值更改(导致控制器功能我必须重定向) 所以我尝试了一些东西,但它没有工作:s) 我希望你帮助我 我的问题是当我选中复选框时,db中没有任何变化
这是我的index.html.twig
<td>
{% if entity.etat==true %}
<div class="switch">
<input id="cmn-toggle-{{ entity.id }}" class="cmn-toggle cmn-toggle-round-flat" checked="true" type="checkbox">
<label class="after" for="cmn-toggle-{{ entity.id }}"></label>
</div>
{% else %}<div class="switch">
<input id="cmn-toggle-{{ entity.id }}" class="cmn-toggle cmn-toggle-round-flat" type="checkbox">
<label class="after" for="cmn-toggle-{{ entity.id }}"></label>
</div>{% endif %}
<script>
$("input[type='checkbox']").change(function(e) {
var value = $(this).val();
$.ajax({
type: "POST",
url: Routing.generate('statut_root,{'id': entity.id}'),
async: true,
data: $(this).serialize(),
success: function (msg) {
if(data.result == 0) {
for (var key in data.data) {
$(form.find('[name*="'+key+'"]')[0]).before('<ul class="errors"><li>'+data.data[key]+'</li></ul>');
}
} else {
// Submit OK
}
}
});
});
</script>
这是我的控制器
public function StatutAction($id)
{
$em = $this->getDoctrine()->getManager();
$pros = $em->getRepository('SwibBundle:prospect')->find($id);
if($pros->getEtat()==true){
$pros->setEtat(false);
}else if($pros->getEtat()==false){
$pros->setEtat(true);
}
$em->persist($pros);
$em->flush();
if ( $request->isXmlHttpRequest() ) {
if (!$form->isValid()) {
return array(
'result' => 0,
'message' => 'Invalid form',
'data' => $this->getErrorMessages($form)
);
return array(
'result' => 1,
'message' => 'ok',
'data' => ''
);}
}
}
protected function getErrorMessages(\Symfony\Component\Form\Form $form)
{
$errors = array();
foreach ($form->getErrors() as $key => $error) {
$errors[] = $error->getMessage();
}
foreach ($form->all() as $child) {
if (!$child->isValid()) {
$errors[$child->getName()] = $this->getErrorMessages($child);
}
}
return $errors;
}
这是我的路由器
statut_root:
path: /statut/{id}
defaults: { _controller: SwibBundle:prospect:Statut}