symfony - 在 Twig 中显示 Flash 消息的正确方法

标签 symfony twig

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

(DBALexception, PDOexception,SQLexceoption,etc) [2/2] DBALException: An exception occurred while executing 'INSERT INTO voters ( blah and blah......)

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

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

我在 Controller 中有这个

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

在 Twig 中

{% 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 页面。否则,它将创建一条成功闪现消息。我也想这样做html5 验证方式,其中每个错误都以表单呈现

enter image description here

最佳答案

以 YML 格式创建约束

例如

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

关于symfony - 在 Twig 中显示 Flash 消息的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32042253/

相关文章:

symfony - 在 Twig 模板中显示 symfony 对象值

Symfony2 : phpinfo() using a twig template for layout?

postgresql - 如何使用 Doctrine 和 Symfony 即时设置 PostgreSQL 模式?

php - Symfony2 + Doctrine 管理大尺寸数据生成错误 : Error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 5131594 bytes)

symfony - 将变量传递到模板名称中包含变量的包含的 Twig 模板中

Symfony 渲染 Controller 异常模板 ("")

arrays - Symfony 翻译消息与数组,查找长度

regex - Symfony 正则表达式表单验证

php - 在 Symfony2 配置 TreeBuilder 中使用正则表达式验证?

symfony - Avalanche Imagine Bundle 如何在生产环境中运行?