在Twig中显示flash消息的正确方法

时间:2015-08-17 03:11:36

标签: symfony twig

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

  

(DBALexception,PDOexception,SQLexceoption等)   [2/2] DBALException:执行'INSERT INTO选民(blah and blah ......)时发生异常

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

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

我在控制器中有这个

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

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

enter image description here

2 个答案:

答案 0 :(得分:2)

以YML格式创建约束

例如

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

答案 1 :(得分:1)

你不能这样做。例外&#39;突破&#39;程序的流程,您只需在遇到代码时继续执行代码即可 但是,您可以手动捕获代码中的异常,然后显示出现的错误:

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