This is related to How to overrule Craft error messages with custom setError()? but I don't have enough rep to comment on it.

I have a plugin that needs to check if a user is ok to be deleted before doing so. Stopping the delete with $event->performAction = false; works fine, but I am unable to overwrite the Craft message with my own to tell them why.

The following code demonstrates the problem:

craft()->on('users.onBeforeDeleteUser', function(Event $event){
    craft()->userSession->setError("I don't want to delete " . $event->params['user']->username);
    $event->performAction = false;

If deleting from the edit user page the "Couldn't delete the user" message is shown. I can change setError() to setNotice() and it will be displayed alongside the Craft error as a potential workaround, but I'm reporting an error so would like to display it as such. (There is the other workaround of setting the error on a field of the user entry, but in my case the error has nothing to do with a field on the page so this would be misleading.)

If deleting from the users listing page setError() shows nothing, just the Craft notice "Deleted Users" is shown. This also cannot be overridden with setNotice().

Is there a way of overriding the Craft error message or showing my own error message alongside it?

1 Answer 1


I can't think of a clean way to do this.

craft()->userSession->setError() is just a wrapper for craft()->userSession->setFlash() that uses a key of error and saves the message to user session and is good for one request. Multiple calls to it just overwrite the same key causing the last one in to win.

You could maybe use setFlash() directly, passing in your own key, then add some JS for displaying the additional flash message similar to how Craft does it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.