Search code examples
symfonytwig

Correct way of displaying flash messages in Twig


I have a registration form, after submit,show flash messages and redirect to specific pages.My flash success messages works fine if no errors found.However, error messages will not show up, when there is an error example blank field, but instead the default Symfony2 exceptions displays.

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

This is in development stage,I want to test error messages and I want to display the form again and the error flashes instead of Symfony2 500 error page;

How to temporarily disable Symfony2 exceptions in specific pages and instead show up the error flash messages during development?

I have this in 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(),
    ));

In 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) }}

So instead of showing Symfony2 exceptions, I want to redirect back to the form and show individual form errors if form submitting failed

In Symfony 1.4 , I can easily do it in form.class which extends the base class like this

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

And it will create a flash error message in web page if there is an error when filling the form and redisplay the form instead of redirecting to 501 page.Otherwise it will create a success flash message.I want to do it the same way html5 validation,where every error is rendered in form

enter image description here


Solution

  • Create a constraint in YML format

    for example

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