Flash-Message Service & error-block

The Flash-Builder writes a message to the Session, which can be displayed exactly once by the user. This service (flash_msg) is a short version for $this->container->get('session')->getFlashBag() which helps to format the message.

There are some different possibilities to set the Flash-Message with the translation:

  • use the default translations (recommended only for already translated text, “notice” is the default tag, which is used as html-class to display)
$this->container->get('flash_msg')->set('Message-Text');
  • the message-type is ‘danger’
$this->container->get('flash_msg')->set('Message-Text', 'danger');
  • the string ‘translation_domain’ is used as Translation-Domain to translate the “Message-Text”-String.
$this->container->get('flash_msg')->set('Message-Text', 'info', 'translation_domain');
  • The the first Array (%replace%-Key) tells the Translator to replace the %replace%-string with the given “Text”. In the “translation_domain”-Translation-Messages, the string “Message-Text-Replace” is translated as the Text is replaced -> Text.
$this->container->get('flash_msg')->set('Message-Text-Replace', 'danger', array(['%replace%' => 'Text'], 'translation_domain'));
# translation_domain.en.yml
Message-Text-Replace: "the Text is replaced -> %replace%"

Bootstrap 3 supports the alert-classes (“success”, “info”, “warning”, “danger”). This are the possible $type-vars, which are inserted directly into the div-class (<div class="alert alert-{{ flashMessage.class }}">)

Note

The Flash-Message is displayed automatically in the twig-templage layout.html.twig (tpointCoreBundle). The Flash-Block ({% block flashBlock %}) is located inside the {% block content %} but outside the {% block body %}.

This means, that the Flash-Message would not be displayed, if there is some “full-size-contents” (Twig-Template uses the content-Block (all Page Contents) or the mainContent-Block (anything after the Navbar) to display the Content).

Error-Messages

The var $errors (must be an array) can be added to the render()-method in the controller. Each array-element is rendered as new <p> in the alert-div.

<div class="alert alert-danger">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    {% for error in errors %}
        <p>{{ error }}</p>
    {% endfor %}
</div>

Note

As you can see, the messages are not translated. You have to pass the translated strings to the error-array.