Reputation: 470
I'm working with symfony 2.7 form component to update an entity called TapsAlert
.
The problem, is the entity is not updated after submitting the form.
I don't know what is going wrong. Here's a part of code:
public function editarRegistroAction(Request $request, $id){
$em = $this->getDoctrine()->getManager();
$altd = $em->getRepository('ModeloBundle:TapsAlert')->find($id);
$altd->setAsunto($altd->getAsunto());
$altd->setGls($altd->getGls());
$altd->setDestinatario($altd->getDestinatario());
$altd->setPrts($altd->getPrts());
$altd->setTags($altd->getTags());
$altd->setValor($altd->getValor());
$form = $this->createFormBuilder($altd)
->add('Asunto', 'text')
->add('gls', 'text')
->add('destinatario', 'text')
//->add('dias', 'number')
->add('prts', 'text')
->add('Tags', 'text')
->add('valor', 'text')
->add('save', 'submit', array('label' => 'Guardar Cambios'))
->getForm();
//$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//$edAlert = $form->getData();
$em = $this->getDoctrine()->getManager();
//$em->persist($altd);
$em->flush();
return $this->redirect('http://192.168.1.128/');
}
return $this->render('MotoBundle:Default:edit.html.twig', array('form' => $form->createView()));
}
Thanks in advance.
Upvotes: 2
Views: 122
Reputation: 2290
First: this part of code is useless. It should be removed:
$altd->setAsunto($altd->getAsunto());
$altd->setGls($altd->getGls());
$altd->setDestinatario($altd->getDestinatario());
$altd->setPrts($altd->getPrts());
$altd->setTags($altd->getTags());
$altd->setValor($altd->getValor());
Second: You are not handling your request:
public function editarRegistroAction(Request $request, $id){
$em = $this->getDoctrine()->getManager();
$altd = $em->getRepository('ModeloBundle:TapsAlert')->find($id);
$form = $this->createFormBuilder($altd)
->add('Asunto', 'text')
->add('gls', 'text')
->add('destinatario', 'text')
//->add('dias', 'number')
->add('prts', 'text')
->add('Tags', 'text')
->add('valor', 'text')
->add('save', 'submit', array('label' => 'Guardar Cambios'))
->getForm();
$form->handleRequest($request); //uncomment this line
if ($form->isSubmitted() && $form->isValid()) {
$em->flush();
return $this->redirect('http://192.168.1.128/'); // This url should an be an external url
}
return $this->render('MotoBundle:Default:edit.html.twig', array('form' => $form->createView()));
}
Third: I beleive that you can improve your code, with paramConvertor:
public function editarRegistroAction(Request $request, TapsAlert $tapsAlert){
$form = $this->createFormBuilder($tapsAlert)
->add('Asunto', 'text')
->add('gls', 'text')
->add('destinatario', 'text')
//->add('dias', 'number')
->add('prts', 'text')
->add('Tags', 'text')
->add('valor', 'text')
->add('save', 'submit', array('label' => 'Guardar Cambios'))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirect('http://192.168.1.128/'); // This url should an be an external url
}
return $this->render('MotoBundle:Default:edit.html.twig', array('form' => $form->createView()));
}
It's a better practice, also, to build the form in a separate class, for more details, take a look to the official documentation https://symfony.com/doc/current/forms.html#creating-form-classes
Upvotes: 2