如何在 Symfony 的同一页面上加载响应

How to load response on the same page in Symfony

我想在同一页面上输出响应。这就是我现在拥有的:

return new Response('ERROR! You cannot edit an inactive Payroll Period');

但我希望它在同一页上。

public function editAction($payrollperiodid)
    {
        $em = $this->getDoctrine()->getManager();

       //$entity = $em->getRepository('comtwclagripayrollBundle:Payrollperiod')->find($payrollperiodid );
       $entity = $em->getRepository('comtwclagripayrollBundle:Payrollperiod')->findOneBy(['payrollperiodid' => $payrollperiodid, 'state' => 0]);

        if (!$entity) {

         return new Response('ERROR! You cannot edit an inactive Payroll Period');
         //return $this->redirect($this->generateUrl('payrollperiod'));

        }

        $editForm = $this->createEditForm($entity);
        $deleteForm = $this->createDeleteForm($payrollperiodid);

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        );
    }

当用户 select 处于非活动状态进行编辑时,这会给他们一个错误。但我不希望输出在同一页上。

您可以在 Action 的参数中使用:

    public function editAction(Request $request, $payrollperiodid)

确保添加 use 语句:

   use Symfony\Component\HttpFoundation\Request;

然后你可以这样做:

    $request->getSession()->getFlashBag()->add('success','
     The message you want to be displayed');
    return $this->redirectToRoute('NameOfTheRoute', array('parameterName' => 'parameterValue'));

然后在 twig 中获取您可以执行的消息:

    {% for flashMessage in app.session.flashbag.get('success') %}
        <div class="alert alert-success" role="alert">
            {{ flashMessage }}
        </div>
    {% endfor %}

这可以帮到你!

使用$this->addFlash()方法;

public function editAction($payrollperiodid)
    {
        $em = $this->getDoctrine()->getManager();

       //$entity = $em->getRepository('comtwclagripayrollBundle:Payrollperiod')->find($payrollperiodid );
       $entity = $em->getRepository('comtwclagripayrollBundle:Payrollperiod')->findOneBy(['payrollperiodid' => $payrollperiodid, 'state' => 0]);

        if (!$entity) {

         $this->addFlash('error', 'You cannot edit an inactive Payroll Period');
         return $this->redirect($this->generateUrl('payrollperiod'));

        }

        $editForm = $this->createEditForm($entity);
        $deleteForm = $this->createDeleteForm($payrollperiodid);

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        );
    }