"Throw" Info message in CMS

silverstripe-4
Tags: #<Tag:0x00007f10d2ecc838>

#1

I want to give the user some message for using my application. I have a complex code logic, which has to be exectued. But in some cases I want to inform the user, that he should do things do before others. So I need a possibility to send messages to the cms without stopping to execute my code.

I tried throw new ValidationException(“message”);
But that stops saving newly inserted field-content…

Is there a way to drop messages in the cms without any consquences to the code-execution?

Thanks in advance
Fabi

// Include any relevant code. If you have a lot of code, link to a gist instead.

#2

You have a few choices, depending on the exact application. You can use a LiteralField to add arbitrary content in a form. You can access the ‘toast’ notifications with a bit of JS (have a look at the code here for examples: GitHub - DorsetDigital/silverstripe-devwarning: Reminds you if you left your SilverStripe site in dev mode)

Alternatively, you can load in any JS you want and run all kinds of custom things (see Preview – SilverStripe Documentation) for details.


#3

If you want to return the message as the response to an AJAX call, just simply do below in your controller (where the error occurs)

return $this->httpError(500, 'Please do this before do that');

change the error code to whatever suitable