<p>I can't think of a clean way to do this.</p>

<p><code>craft()-&gt;userSession-&gt;setError()</code> is just a wrapper for <code>craft()-&gt;userSession-&gt;setFlash()</code> that uses a key of <code>error</code> 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.</p>

<p>You could maybe use <code>setFlash()</code> directly, passing in your own key, then add some <a href="https://github.com/lindseydiloreto/craft-cpcss" rel="nofollow noreferrer">JS</a> for displaying the additional flash message similar to how Craft does it.</p>

CC BY-SA 3.0
2017-10-05T17:44:46.403Z
2017-10-05T17:44:46.403Z
Brad Bell
<p>This is related to <a href="https://craftcms.stackexchange.com/questions/19946/how-to-overrule-craft-error-messages-with-custom-seterror">How to overrule Craft error messages with custom setError()?</a> but I don't have enough rep to comment on it.</p>

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

<p>The following code demonstrates the problem:</p>

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

<p>If deleting from the edit user page the "Couldn't delete the user" message is shown. I can change <code>setError()</code> to <code>setNotice()</code> 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.)</p>

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

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

<plugin-development><error-message><events>
Unable to set error message in onBeforeDeleteUser
# Unable to set error message in onBeforeDeleteUser

(Asked by: username_0 on 2017-10-05)

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?

**Tags:** plugin-development, error-message, events

---

## 1 answers

### Answer by username_1 on 2017-10-05. Score: 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](https://github.com/lindseydiloreto/craft-cpcss) for displaying the additional flash message similar to how Craft does it.


---

