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

时间:2017-07-11 14:20:37

标签: php symfony netbeans response

我想在同一页面上输出回复。这就是我现在所拥有的:

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(),
        );
    }

当用户选择非活动状态进行编辑时,会给出错误。但我不希望输出在同一页面上。

2 个答案:

答案 0 :(得分:0)

使用$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(),
        );
    }

答案 1 :(得分:-2)

在你可以使用的行动的论点中:

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

然后在树枝上得到你可以做的信息:

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

这可以帮到你!