Jump to content

Symfony 2 - processing a form in one action, display in another


Recommended Posts

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'));
            }
        }
    }

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 :P Then you don't have write the complete form object into a session.

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 :)

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.
 

  • 2 weeks later...

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.

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 by moylin
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.