0

I am using the FOSUserBundle in Symfony 3.4. Everything is working correctly. I am overriding the FOSUserBundle templates with my own. The way I am checking if the user failed to login because of an incorrect password or email is this way:

login.html.twig

{% trans_default_domain 'FOSUserBundle' %}
...Some html nothing fancy
                {% if error %}
                    <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
                           {% endif %}

    ....Rest of file content

    </div>

This runs no problem. The user of course is redirected to the index page after failure handled via my security file.
However, I tried using the if statement above in the index.html.twig, it thinks it is undefined. I want to use it so I can for example, when the user gets redirected to index page for my customized message to appear. Is there a for the "error" variable in that twig file to be made global to all twig files when it gets set?

1 Answer 1

0

The FosUserBundle stores errors in the symfony flashBag you can use this code

    {% if app.request.hasPreviousSession %}
        {% for type, messages in app.session.flashbag.all() %}
            {% for message in messages %}
                <div class="flash-{{ type }}">
                    {{ message }}
                </div>
            {% endfor %}
        {% endfor %}
    {% endif %}

You can look this the template of FosUserBundle https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/views/layout.html.twig

Or more globaly https://symfony.com/doc/current/controller.html#flash-messages

If you want to change the default errors message, you should rather replace the default translations of the FosUserBundle.

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

Comments

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.