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:
$this->container->get('flash_msg')->set('Message-Text');
$this->container->get('flash_msg')->set('Message-Text', 'danger');
$this->container->get('flash_msg')->set('Message-Text', 'info', 'translation_domain');
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).
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">×</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.