EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

Auto Close Flash Messages

Open rernesto opened this issue 7 years ago • 9 comments

I don't know if this could be useful or not to somebody else, but I was missing at least the close button for flash notifications. I did it overriding vendor/easycorp/easyadmin-bundle/src/Resources/views/default/flash_messages.html.twig using Symfony overriding mechanism with templates/bundles/EasyAdminBundle/default/flash_messages.html.twig.

{% if app.session is not null and app.session.started %}
    {% set _flash_messages = app.session.flashbag.all %}

    {% if _flash_messages|length > 0 %}
        <div id="flash-messages">
            {% for label, messages in _flash_messages %}
                {% for message in messages %}
                    <div class="alert alert-{{ label }}" role="alert">
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                        {{ message|trans|striptags }}
                    </div>
                {% endfor %}
            {% endfor %}
        </div>
    {% endif %}
{% endif %}

Also a JavaScript code was added to auto close the bootstrap alert:

// Auto Close Bootstrap alerts
window.setTimeout(function() {
    $(".alert").fadeTo(500, 0).slideUp(500, function(){
        $(this).remove();
    });
}, 4000);

rernesto avatar Apr 05 '19 11:04 rernesto

I think it's not an issue, and you should create PR. People will say whether this is needed, and it will be merged or not. Sounds like a nice feature.

jazithedev avatar Apr 07 '19 09:04 jazithedev

So far I am fine extending til @javiereguiluz add features similar to the ones on my own, I have no time to keep my own fork from easyadmin repository and doing the PRs. The issues threads helps me a lot as Knowledge Base. This is an awesome community ;)

rernesto avatar Apr 07 '19 17:04 rernesto

@rernesto @javiereguiluz Ok, so I'll try to implement it and create proper PR.

jazithedev avatar Apr 15 '19 20:04 jazithedev

@javiereguiluz, I've got one question. IF we want to add closing flash messages functionality from Boostrap, there is need to add bootstrap-all.js to assets. Because all Javascript code related with those alerts is in there. Without it, closing alerts does not work and clicking the "X" sign does nothing.

So... adding this "Hide flash" button to flash_messages.html.twig has no sense if proper Javascript is not added.

There are two solutions then. First: let's leave this as it is now (do not add this functionality). Second: let's separate the part of Javascript code responsible for doing such things and add it to the base file which is added by default to EasyAdmin.

Thanks in advance for your reponse!

jazithedev avatar Apr 16 '19 16:04 jazithedev

How to enable the flash messages in EasyAdmin 3? For me, the messages are not displaying at all (whether you add a new entry, update, or delete existing entries). What I might be missing?

MagePsycho avatar Dec 26 '20 13:12 MagePsycho

I don't know, I just set a flash message an as soon as EA is rendering a new page it shows on the top image

parijke avatar Dec 27 '20 13:12 parijke

@MagePsycho there's been a misunderstanding: EasyAdmin supports displaying flash messages (if your controllers create flash messages, you'll see them without having to change anything in your templates.) ... but EasyAdmin doesn't display any flash messages itself by default.

Why do we do this? Because we don't want to display flash messages when things work. That's something which apps did in the past ... but we consider it annoying. Imagine your browser displaying a flash message saying "Web page was loaded successfully" or Microsoft Word displaying a flash message saying "Bold style was successfully applied to the selected text".

So, our policy is: don't display flash messages when things work ... but display error messages when things doesn't work.

javiereguiluz avatar Dec 28 '20 08:12 javiereguiluz

thanks Sir

dilpreet1999 avatar Feb 14 '21 15:02 dilpreet1999

Comment personnalisé les messages flash dans easyadmin 4

Penaldo-Gouclounon avatar Aug 22 '23 16:08 Penaldo-Gouclounon