1

I have a form, which has to be passed by some other validations than unusual (about 4 fields are depending from each other). Thing is, when its failed, I redirect the user back, but then the form loses its values, I dont want it. I know it can be done with session, but there might be a "sanitier" way. Code is usual:

public function printAction()
{
    if ($this->getRequest()->getMethod() == "POST")
    {
        $form->bindRequest($this->getRequest());
        if ($form->isValid())
        {
             .... more validation.... Failed!
             return $this->redirect($this->generateUrl("SiteHomePeltexStockStockHistory_print"));
             // and this is when I lose the values.... I dont want it
        }
    }
}
Elnur Abdurrakhimov
  • 44,533
  • 10
  • 148
  • 133
John Smitth
  • 121
  • 2
  • 12

3 Answers3

2

You can use the same action for both GET and POST requests related to a form. If validation fails, just don't redirect and the same form will be redisplayed with entered values and validation error messages:

/**
 * @Template
 */
public function addAction(Request $request)
{
    $form = /* ... */;        

    if ($request->isMethod('POST')) {
        $form->bind($request);

        if ($form->isValid()) {
            // do something and redirect
        }

        // the form is not valid, so do nothing and the form will be redisplayed
    }

    return [
        'form' => $form->createView(),
    ];
}
Elnur Abdurrakhimov
  • 44,533
  • 10
  • 148
  • 133
  • kind of off-topic, can you edit your answer here http://stackoverflow.com/questions/10025278/symfony2-bundles-am-i-using-them-right/10025644#10025644, I accidentally downvoted it, and would like to change that vote, thanks. – Alexandrin Rus Aug 30 '13 at 09:22
1

You can passe your parametters to the new page when making the new redirection:

$this->redirect($this->generateUrl('SiteHomePeltexStockStockHistory_print', array('name1' => 'input1', 'name2' => 'input2', 'name3' => $input3, ....)));

or directly pass an array of post values:

$this->redirect($this->generateUrl('SiteHomePeltexStockStockHistory_print', array('values' => $values_array)));
Ali Jabbar
  • 15
  • 1
  • 2
1

You may want to do something like this

class FooController extends Controller
{
    /**
     * @Route("/new")
     * @Method({"GET"})
     */
    public function newAction()
    {
        // This view would send the form content to /create
        return $this->render('YourBundle:form:create.html.twig', array('form' => $form));
    }

    /**
     * @Route("/create")
     * @Method({"POST"})
     */
    public function createAction(Request $request)
    {
        // ... Code
        if ($form->isValid()) {
            if (/* Still valid */) {
                // Whatever you do when validation passed
            }
        }

        // Validation failed, just pass the form
        return $this->render('YourBundle:form:create.html.twig', array('form' => $form));
    }
}
Touki
  • 7,465
  • 3
  • 41
  • 63