在Twig中显示Flash消息的正确方式

问题描述 投票:0回答:3

我有一个注册表单,提交后显示闪现消息并重定向到特定页面。如果没有发现错误,我的闪现成功消息可以正常工作。但是,当存在错误示例空白字段时,错误消息将不会显示,而是显示显示默认的 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
3个回答
2
投票

以 YML 格式创建约束

例如

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

1
投票

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

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

0
投票

在 Twig 文件中执行此操作的一个好方法如下:

    {% for message in app.flashes('name_of_flash_message') %}
        <div class="alert alert-success"> {# using bootstrap #}
            <h3>{{ message }}</h3>
        </div>
    {% endfor %}

确保您在控制器中定义了闪现消息:

例如

$this->addFlash('delete-entity', "客户已被删除。");

© www.soinside.com 2019 - 2024. All rights reserved.