1

I have to be missing something here, just trying to set a flash message in a new project.

In controller:

$this->addFlash(
    'success',
    'Your entry was added!'
);

In my template, I include:

{% for message in app.flashes(['success', 'notice']) %}
    <div class="alert alert-success">
        <p align="center">
            <b>Success! </b><br/>
            {{ message }}
        </p>
    </div>
{% endfor %}

I get the following exception every time:

"An exception has been thrown during the rendering of a template ("Notice: Array to string conversion")."

enter image description here

I checked the length of message and it's zero. If I dump it, it's an empty array as it should be. Anyone see what I'm missing here, driving me crazy. Also, if I set a flash message it gives me the same error.

3
  • What is your symfony version? Look this article. It tells you can filter by multiple type after symfony 3.3. symfony.com/blog/new-in-symfony-3-3-improved-flash-messages Commented Aug 22, 2019 at 20:41
  • Current 3.4 release. I copied the code from that page, so figured it would work. Have never had this problem in any other projects. Commented Aug 22, 2019 at 20:45
  • 2
    In your twig template the for loop message is an array, so change message in messages and add another for loop to show each message (as showed in the docs). Commented Aug 22, 2019 at 21:21

1 Answer 1

4

Read and display several types of flash messages:

{% for label, messages in app.flashes(['success', 'notice']) %}
    {% for message in messages %}
        <div class="alert alert-success">
            <p align="center">
                <b>Success! </b><br/>
                {{ message }}
            </p>
        </div>
    {% endfor %}
{% endfor %}

See: https://symfony.com/doc/current/controller.html#managing-the-session

Sign up to request clarification or add additional context in comments.

1 Comment

Good lord I'm an idiot. 10 straight hours and I apparently can't see the most obvious thing ever... Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.