Symfony 在细枝中显示flash消息的正确方法

Symfony 在细枝中显示flash消息的正确方法,symfony,twig,Symfony,Twig,我有一个注册表,提交后,显示flash消息并重定向到特定页面。如果没有发现错误,我的flash成功消息可以正常工作。但是,当存在错误示例空白字段时,错误消息将不会显示,而是显示默认的Symfony2异常 (DBALexception、PDOexception、sqlExcepOption等) [2/2]DBALException:执行“插入投票者”时发生异常(诸如此类) 这是在开发阶段,我想测试错误消息,我想再次显示表单,错误闪烁而不是SYMFONY2500错误页面 如何在特定页面中临时禁用Sy

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

(DBALexception、PDOexception、sqlExcepOption等) [2/2]DBALException:执行“插入投票者”时发生异常(诸如此类)

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

如何在特定页面中临时禁用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) }}
{%if app.session.flashBag.has('notice')%}
{app.session.flashBag.get('notice')%中的msg为%
{{msg}}
{%endfor%}
{%endif%}
{%if app.session.flashBag.has('error')%}
{app.session.flashBag.get('error')%%中的msg为%
{{msg}}
{%endfor%}
{%endif%}
//registerform.twig.html
{{form_start(form,{attr:{novalidate:'novalidate'}}}}
{{form_errors(form)}}
{{form_行(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>')
));
$this->mergePostValidator(新的sfValidatorDoctrineUnique(数组(
“模型”=>“投票者”,
'column'=>array('firstname','middlename','lastname','city\u id','birth','profession\u id')),array('invalid'=>'您已注册。无需继续buddy')
));
如果填写表单时出现错误,它将在网页中创建一条flash错误消息,并重新显示表单,而不是重定向到501页面。否则,它将创建一条成功的flash消息。我想用html5验证的同样方法,在表单中呈现每个错误


你不能那样做。异常“破坏”了程序的流程,当遇到异常时,不能简单地继续执行代码。
但是,您可以手动捕获代码中的异常,然后像这样显示出错的地方:

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

以YML格式创建约束

比如说

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

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