简体   繁体   English

Symfony2-提交表单时如何保留请求参数?

[英]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). 这是一种情况:使用请求POST参数(即实体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). 提交表单并再次调用操作以修改基础对象的值后,Request对象中的我的实体ID参数消失了(由Symfony Form请求对象代替了)。

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 您只能将其存储在整个应用程序中使用的全局配置变量中,才能将其存储在parameters.yml中

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 有用于显示Flash消息的Flashbag,例如“成功提交表单”或“失败表单提交”,对于下一个请求仅一次有效

i've given you all the options, you can choose accordingly, you can ask me additional details if needed 我给了您所有选择,您可以进行相应选择,如果需要,可以询问我其他详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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