Getting errors back to a template whilst in an event I have a plugin which is trying to create a user whilst inside the Charge plugin's onBeforeCharge event.

Here's the relevant code in MyPlugin.php:

craft()-&gt;on('charge.onBeforeCharge', function(Event $event){
    $newUser = new UserModel;

    $newUser-&gt;firstName = craft()-&gt;request-&gt;getPost('firstName');
    $newUser-&gt;lastName  = craft()-&gt;request-&gt;getPost('lastName');
    $newUser-&gt;username  = craft()-&gt;request-&gt;getPost('username');
    $newUser-&gt;email     = craft()-&gt;request-&gt;getPost('email');
    $newUser-&gt;password  = craft()-&gt;request-&gt;getPost('password');

    if ($newUser-&gt;validate()) {
        // create user
    } else {
        MyPlugin::log('User supplied details were invalid, attempting to redirect with errors.', LogLevel::Error);

        craft()-&gt;urlManager-&gt;setRouteVariables(array(
            'myErrors' =&gt; $newUser
        ));

        $route = craft()-&gt;urlManager-&gt;parseUrl(craft()-&gt;request);
        craft()-&gt;runController($route);
        craft()-&gt;end();
    }
}


My problem: this logs my message to the MyPlugin log, and it redirects the page back to the calling template: but "myErrors" is blank. Nothing available.

What am I doing wrong?
Rather than ending the request in your plugin, you should probably set $event-&gt;performAction = false, add your errors to the charge model and return control to Charge to add it's own error message and return to the calling template template.

So your code should look more like this:

craft()-&gt;on('charge.onBeforeCharge', function(Event $event){
    $newUser = new UserModel;

    $newUser-&gt;firstName = craft()-&gt;request-&gt;getPost('firstName');
    $newUser-&gt;lastName  = craft()-&gt;request-&gt;getPost('lastName');
    $newUser-&gt;username  = craft()-&gt;request-&gt;getPost('username');
    $newUser-&gt;email     = craft()-&gt;request-&gt;getPost('email');
    $newUser-&gt;password  = craft()-&gt;request-&gt;getPost('password');

    if ($newUser-&gt;validate()) {
        // create user
    } else {
        MyPlugin::log('User supplied details were invalid, attempting to redirect with errors.', LogLevel::Error);

        // Add our error message to the charge object
        $chargeModel = $event-&gt;params['charge'];
        $chargeModel-&gt;addError('myErrors', 'Invalid user details supplied');

        // Stop the payment from proceeding
        $event-&gt;performAction = false;
    }
}


In your template you should be able to access the errors by calling charge-&gt;getErrors().

If you supply a parameter to the getErrors() function you will only get those errors back. So if, as above, we add a custom key to the error, we can query for that in the template like this:

{% set myErrors = charge.getErrors('myErrors') %}
{% if myErrors | length %}
    {# Oops! Something went wrong, let's handle that… #}
{% endif %}