简体   繁体   中英

Symfony2 - How to preserve request parameter when submitting form?

Here's the situation : Some form handling action is invoked from twig template with request POST parameter (ie. entity ID). After the form is submitted and action is being invoked once again to modify value of the underlying object, my entity ID parameter from Request object is gone (it's replaced by Symfony Form request object).

Here's the question : What's the best practice to preserve this request parameter between two invokes of the form?

Here's simple code example :

public function editEntityAction(Request $request, $type)
{

    $objId = $request->request->get('entityId');

    $updateObj = null;
    $form = null;

    $dbMen = $dbMen->getRepository('BakaMainBundle:Brand');
    $updateObj = $dbMen->find($objId);
    $form = $this->createForm(new AddBrand(), $updateObj);

    if ($updateObj == null && $form == null)
        $this->redirectToRoute('baka_main_main_index');

    $form->handleRequest($request);

    if ($form->isValid() && $form->isSubmitted())
    {
        $menager = $this->getDoctrine()->getManager();
        $menager->flush();
    }

    return $this->render('@BakaMain/Specs/EditEntity.html.twig',
        array('form' => ($form->createView()));
}

There are three possible ways to preserve values

  1. sessions
  2. cookies
  3. hidden form fields

you could store it in parameters.yml only if its a global configuration variable that is used across the application

there are flashbags used to display flash messages such as 'success form submitted' or 'form submission failed' which is valid exactly once for the next request

i've given you all the options, you can choose accordingly, you can ask me additional details if needed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM