【问题标题】:Correct way of displaying flash messages in Twig在 Twig 中显示 flash 消息的正确方法
【发布时间】:2015-08-17 03:11:35
【问题描述】:

我有一个注册表单,提交后,显示flash消息并重定向到特定页面。如果没有发现错误,我的flash成功消息工作正常。但是,错误消息不会显示,当有错误示例空白字段时,但是会显示默认的 Symfony2 异常。

(DBAL 异常、PDO 异常、SQL 异常选项等) [2/2] DBALException: 执行'INSERT INTO voters (blah and blah......)时发生异常

这是在开发阶段,我想测试错误消息,我想再次显示表单并且错误闪烁而不是 Symfony2 500 错误页面;

如何在特定页面中暂时禁用 Symfony2 异常,而在开发过程中显示错误提示信息?

我在控制器中有这个

if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();


        $this->addFlash('notice', 'Welcome to the growing lists of Supporters, dont forget to share and invite this to your friends and relatives, have a magical day!');

        //return $this->redirect($this->generateUrl('vo_show', array('id' => $entity->getId())));
        return $this->redirect($this->generateUrl('vo'));
    }
    else {

        $this->addFlash('error', 'Welcome to the Death Star, have a magical day!');

        return $this->render('Bundle:Vo:new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    ));

在树枝上

{% if app.session.flashBag.has('notice') %}
        <div class="alert alert-success fade in" role="alert">
            <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
            {% for msg in app.session.flashBag.get('notice') %}
                {{ msg }}
            {% endfor %}
        </div>
    {% endif %}
    {% if app.session.flashBag.has('error') %}
        <div class="alert alert-error fade in" role="alert">
            <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
            {% for msg in app.session.flashBag.get('error') %}
                {{ msg }}
            {% endfor %}
        </div>
    {% endif %}


//registerform.twig.html

{{ form_start(form, {attr: {novalidate: 'novalidate'}} ) }}
        {{ form_errors(form) }}
        {{ form_row(form.comments,{'attr': {'placeholder': 'Why You Want Death'}}) }}
        {{ form_end(form) }}

因此,我不想显示 Symfony2 异常,而是希望重定向回表单并在表单提交失败时显示单个表单错误

在 Symfony 1.4 中,我可以在 form.class 中轻松完成,它像这样扩展基类

$this->mergePostValidator(new sfValidatorDoctrineUnique(array(
  'model' => 'Voters',
  'column' => array('firstname', 'middlename', 'lastname', 'city_id', 'birthday', 'profession_id')),array('invalid' => '<div class="alert alert-warning">You are already registered.No need to proceed buddy</div>')
));

如果填写表单时出现错误,它会在网页中创建一个flash错误消息并重新显示表单而不是重定向到501页面。否则它将创建一个成功的flash消息。我也想这样做html5验证方式,每个错误都以表单呈现

【问题讨论】:

    标签: symfony twig


    【解决方案1】:

    以 YML 格式创建约束

    例如

    Project\Bundle\YourBundle\Entity\Vo:
    properties:
        firstname:
            - NotBlank: ~
    

    【讨论】:

      【解决方案2】:

      你不能那样做。异常会“破坏”程序流程,您不能在遇到异常时简单地继续执行代码。
      但是,您可以手动在代码中捕获异常,然后显示如下错误:

      if ($form->isValid()) {
          try{
          $em = $this->getDoctrine()->getManager();
          $em->persist($entity);
          $em->flush();
          } catch(\Exception $e){
              $this->addFlash('notice', sprintf('A %s was thrown when trying to persist 
                            the entities with message = %s', get_class($e), $e->getMessage());
         }
      // Rest of your code ...
      // Take note that when you encounter exception the flow of the program
      // is 'broken' and you must carefully examine what happened to avoid 
      // unintended consequences and stuff like null valued variables
      

      【讨论】:

      • 理想情况下,您不应向最终用户显示异常消息。异常消息有时包含有关应用程序内部的敏感信息,可用于篡改/修改/破解应用程序本身。开发人员应记录异常消息。
      • 那么在不重定向到 501 错误页面的情况下呈现单个错误消息的正确方法是什么?而不是重定向回表单?
      • AlanChavez 是对的,不要在生产中使用它。如果您不想要 5** 页,您可以采用与上述类似的方法,但不是显示异常数据,而是显示一些自定义错误消息,例如“我们在处理您的表单时遇到问题,请联系管理员”或等等
      • 我的问题是不会显示错误通知,而是 Symfony2 异常并重定向到 501 页面,将呈现整个页面的错误。这在开发阶段很有帮助,但我也想要显示 flash消息,成功和错误都闪烁。成功闪烁成功,而错误不闪烁。在 Symfony 4 中,我可以在表单类中做到这一点
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多