Jessica Posted May 19, 2013 Share Posted May 19, 2013 I may be going about this all wrong, so feel free to tell me This is my first attempt at using Symfony. I have a controller which generates a form in one action (My "view all" page, it displays both the list of existing and the form to create) and the form processing is in another action ("add action"). In the add action I process the form and add if it's valid. Whether or not it's valid you get redirected back to the view all page to see the form again. I want the processing to be a different action/route in order to prevent submitting again upon refresh, and I just prefer the processing in a different action. The problem is when there is an error, the form doesn't display it, because after redirecting back to the view all action, the form's data is gone. How do I persist the errors/values/etc over multiple actions? I can't find any examples of this in Symfony, it looks like they always do the processing in the same action. I thought first to put the form in the session, but I don't want to store a ton of form objects manually if there's a better way to do this. public function viewAllAction() { $repository = $this->getDoctrine()->getRepository('SageCoreBundle:Service'); $services = $repository->findBy(array('active' => 1)); $this->form = $this->createForm(new ServiceType, null); return $this->render('SageAdminBundle:Service:viewAll.html.twig', array( 'services' => $services, 'form' => $this->form->createView() ) ); } public function addAction(Request $request) { $this->form = $this->createForm(new ServiceType, null); if ($request->isMethod('POST')) { $form->bind($request); if ($form->isValid()) { $service = $form->getData(); $service->setActive(1); $em = $this->getDoctrine()->getManager(); $em->persist($service); $em->flush(); $this->get('session')->getFlashBag()->add('success', 'Service Added!'); return $this->redirect($this->generateUrl('admin_services')); } else { $this->get('session')->getFlashBag()->add('error', 'Error'); return $this->redirect($this->generateUrl('admin_services')); } } } Quote Link to comment Share on other sites More sharing options...
Mad programmer Posted May 20, 2013 Share Posted May 20, 2013 Why not just write the form values from the request object to the session? And in your view you bind those values to the form again? I cannot think of any way why that should not work Then you don't have write the complete form object into a session. Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 20, 2013 Author Share Posted May 20, 2013 Because then I don't get the form errors without having to run the validation again. Quote Link to comment Share on other sites More sharing options...
Mad programmer Posted May 20, 2013 Share Posted May 20, 2013 True, But how about getting the form validators output first? And retrieve the values only and store those along with the request parameters in the session? That way you don't have to run the validators again, and you have the data Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 20, 2013 Author Share Posted May 20, 2013 Because that's a ton of work that Symfony handles for me right now. Do you use Symfony? Quote Link to comment Share on other sites More sharing options...
Mad programmer Posted May 20, 2013 Share Posted May 20, 2013 Well I do use symfony's components, I am looking into the full stack framework but currently I am not using it for a project or anything.Maybe you can write a service that does the job? It may be a little more work right now but It will safe time if you need this particular function more then once in your application. Quote Link to comment Share on other sites More sharing options...
moylin Posted May 28, 2013 Share Posted May 28, 2013 Based on what you're wanting, it sounds like forwarding is what you're looking for. http://symfony.com/doc/2.0/book/controller.html#forwarding Quote Link to comment Share on other sites More sharing options...
Pierre-Yves Posted June 6, 2013 Share Posted June 6, 2013 Based on what you're wanting, it sounds like forwarding is what you're looking for. http://symfony.com/doc/2.0/book/controller.html#forwarding Forward is not what she's looking for because of this : I want the processing to be a different action/route in order to prevent submitting again upon refresh I think forward will trigger the form submit upon refresh because this will be a POST request and the browser will try to POST again when you hit refresh. There is no easy way to do this. We always do the processing in the same action and don't care about the form being submitted again upon refresh because most of the time (not to say always), when there is an error in the form, the user will want to correct his input and submit the form again. Quote Link to comment Share on other sites More sharing options...
moylin Posted June 6, 2013 Share Posted June 6, 2013 (edited) This type of forwarding isn't to a new page, just to the controler you want, so you'd forward back to the controller you used to display the form, and you'd pass it your already managed form object to display. This will keep the processing URL the same, but allow the form to be rendered again without a redirect and saving all the info in the session. Edited June 6, 2013 by moylin Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.