This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

zs60815
zs60815

Hi, I thought I understood how Flash messages are working, but looks like I didn't. I have:

$(document).on('ajaxSetup', function(event, context) {
// Enable AJAX handling of Flash messages on all AJAX requests
context.options.flash = true

// Enable the StripeLoadIndicator on all AJAX requests
context.options.loading = $.oc.stripeLoadIndicator

// Handle Error Messages by triggering a flashMsg of type error
context.options.handleErrorMessage = function(message) {
    $.oc.flashMsg({ text: message, class: 'error' })
}

// Handle Flash Messages by triggering a flashMsg of the message type
context.options.handleFlashMessage = function(message, type) {
    $.oc.flashMsg({ text: message, class: type })
}
})

at the end of my layout files, and it's working as expected when using AJAX, but now, when I try to redirect to Home page, with a Flash message, instead of displaying "No records" on the page, it doesn't work.

I have this, in a CMS page PHP section:

    if(isset($node)){
    $this['record'] = $node;
} else {
    return Redirect::to('/')->with('message', 'Page not found!');
}

In the home page php section:

    function onStart()
{
    $message = Session::get('message');
    if(isset($message)){
        $message = Message::trans($message);
        Flash::info($message);
        Session::forget('message');
    } 
}

This doesn't display anything untill I don't put:

    {% flash %}
    <p
        data-control="flash-message"
        class="flash-message fade {{ type }}"
        data-interval="5">
        {{ message }}
    </p>
{% endflash %}

in the markup section. Where I use AJAX, it works without...

But then, with the above code in the markup section, the message is displayed and if I rerfesh the page it's displayed again, once more. If I open /backend instead of refresh it's also displayed....

What am I missing here..?

Last updated

mjauvin
mjauvin

Just add data-request-flash to your form tag as described here:

https://octobercms.com/docs/ajax/extras#ajax-flash

zs60815
zs60815

mjauvin said:

Just add data-request-flash to your form tag as described here:

https://octobercms.com/docs/ajax/extras#ajax-flash

Thanks, but I'm not redirecting from a form, I redirect from a page php section:

else {
return Redirect::to('/')->with('message', 'Page not found!');
}
mjauvin
mjauvin

Never mind my comment, I misread your initial post

mjauvin
mjauvin

Did you figure it out? What happens if you remove the onStart() method on your homepage but leave the markup code?

zs60815
zs60815

mjauvin said:

Did you figure it out? What happens if you remove the onStart() method on your homepage but leave the markup code?

I had to no time to figure it out. If I remove the the onStart() method, no message is displayed....

JeffGoldblum
JeffGoldblum

If you want to display a flash message after a redirect, just Flash the message before sending the redirect and make sure you have the static flash code ready to display the message on page load.

if (isset($node)) {
    $this['record'] = $node;
} else {
    Flash::info('Page not found');
    return Redirect::to('/');
}
{% flash %}
    <p>
        data-control="flash-message"
        class="flash-message fade {{ type }}"
        data-interval="5">
        {{ message }}
    </p>
{% endflash %}

Also, I'm not sure what you're using the flash message for, but it's probably not the best way of handling it. Flash messages should generally only be used for information that might be of mild interest to the user, but doesn't really matter if they don't see it before it disappears; and redirecting back with a flash message of "record not found" does not sound like a great UX

zs60815
zs60815

LukeTowers said:

If you want to display a flash message after a redirect, just Flash the message before sending the redirect and make sure you have the static flash code ready to display the message on page load.

if (isset($node)) {
   $this['record'] = $node;
} else {
   Flash::info('Page not found');
   return Redirect::to('/');
}
{% flash %}
   <p>
       data-control="flash-message"
       class="flash-message fade {{ type }}"
       data-interval="5">
       {{ message }}
   </p>
{% endflash %}

Also, I'm not sure what you're using the flash message for, but it's probably not the best way of handling it. Flash messages should generally only be used for information that might be of mild interest to the user, but doesn't really matter if they don't see it before it disappears; and redirecting back with a flash message of "record not found" does not sound like a great UX

Thank you. This works as expected. Basically this message will be displayed only if someone visits the site on some old, none exsistent url, but url is not completly broken to display 404 error page...

1-8 of 8

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.