<p>Rather than ending the request in your plugin, you should probably set <code>$event-&gt;performAction = false</code>, add your errors to the <code>charge</code> model and return control to <code>Charge</code> to add it's own error message and return to the calling template template.</p>

<p>So your code should look more like this:</p>

<pre><code>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;
    }
}
</code></pre>

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

<p>If you supply a parameter to the <code>getErrors()</code> 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:</p>

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

CC BY-SA 3.0
2016-06-10T09:23:35.280Z
That half works - the problem being Charge carries onto the onCharge event, and I need it to not do that. I need it to return an error from the onBeforeCharge event otherwise it bombs out hard because data required at the onCharge event doesn't exist.
Matt Wilcox
CC BY-SA 3.0
2016-06-10T13:12:28.610Z
Ah, I can see that the handleCharge controller is adding extra errors before returning to the template, which infers that there was a problem with the payment, even though payment is never attempted. You could resolve that in your template by checking for the existence of your custom error message first and suppressing the other errors if yours exists.
Steve Rowling
CC BY-SA 3.0
2016-06-10T13:54:37.960Z
I've since changed the code slightly to ...

    $chargeModel = $event->params['charge'];
    $chargeModel->addError('general', 'Invalid user details supplied');

And things are displaying as I need... the problem is, even if I just use "$event->performAction = false;" right at the start of both the onBeforeCharge and onCharge event, it sails through Charge anyway and tries the whole Stripe thing. I need it to stop where I tell it :/
Matt Wilcox
CC BY-SA 3.0
2016-06-10T17:18:31.963Z
What version of Craft are you using? I've just found this question (from you actually!) that might be relevant. http://craftcms.stackexchange.com/questions/7064/event-performaction-false-doesnt-stop-action I doubt you're using a version of Craft that old, but I though I had better check as the problem seems so similar!
Steve Rowling
CC BY-SA 3.0
2016-06-13T08:26:54.700Z
Ha! I have no memory of that at all; but it looks *exactly* like the problem. Current project is not quite up-to-date, but it's only three revisions out. It's been a PITA build that's taking money so we're not wanting to upset the apple cart until the payments are complete.
Matt Wilcox
CC BY-SA 3.0
2016-06-13T08:32:46.637Z
It was a bug in Charge! It was fixed in 2.0.3 but we were running 2.0.2 - it did not support performAction false.
Matt Wilcox
CC BY-SA 3.0
2016-06-13T10:50:06.517Z
Great! Glad you got it fixed!
Steve Rowling
CC BY-SA 3.0
2016-06-09T19:20:30.743Z
2016-06-09T19:20:30.743Z
Steve Rowling
<p>I have a plugin which is trying to create a user whilst inside the Charge plugin's onBeforeCharge event.</p>

<p>Here's the relevant code in MyPlugin.php:</p>

<pre><code>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();
    }
}
</code></pre>

<p>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.</p>

<p>What am I doing wrong?</p>

<plugin-development>
Getting errors back to a template whilst in an event
# Getting errors back to a template whilst in an event

(Asked by: username_0 on 2016-06-09)

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()->on('charge.onBeforeCharge', function(Event $event){
    $newUser = new UserModel;

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

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

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

        $route = craft()->urlManager->parseUrl(craft()->request);
        craft()->runController($route);
        craft()->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?

**Tags:** plugin-development

---

## 1 answers

### Answer by username_1 on 2016-06-09. Score: 2 | Accepted answer

Rather than ending the request in your plugin, you should probably set $event->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()->on('charge.onBeforeCharge', function(Event $event){
    $newUser = new UserModel;

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

    if ($newUser->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->params['charge'];
        $chargeModel->addError('myErrors', 'Invalid user details supplied');

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

In your template you should be able to access the errors by calling charge->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 %}

> **Comments:**
> **username_0**: That half works - the problem being Charge carries onto the onCharge event, and I need it to not do that. I need it to return an error from the onBeforeCharge event otherwise it bombs out hard because data required at the onCharge event doesn't exist.  
> **username_1**: Ah, I can see that the handleCharge controller is adding extra errors before returning to the template, which infers that there was a problem with the payment, even though payment is never attempted. You could resolve that in your template by checking for the existence of your custom error message first and suppressing the other errors if yours exists.  
> **username_0**: I've since changed the code slightly to ...      $chargeModel = $event->params['charge'];     $chargeModel->addError('general', 'Invalid user details supplied');  And things are displaying as I need... the problem is, even if I just use "$event->performAction = false;" right at the start of both the onBeforeCharge and onCharge event, it sails through Charge anyway and tries the whole Stripe thing. I need it to stop where I tell it :/  
> **username_1**: What version of Craft are you using? I've just found this question (from you actually!) that might be relevant. http://craftcms.stackexchange.com/questions/7064/event-performaction-false-doesnt-stop-action I doubt you're using a version of Craft that old, but I though I had better check as the problem seems so similar!  
> **username_0**: Ha! I have no memory of that at all; but it looks *exactly* like the problem. Current project is not quite up-to-date, but it's only three revisions out. It's been a PITA build that's taking money so we're not wanting to upset the apple cart until the payments are complete.  
> **username_0**: It was a bug in Charge! It was fixed in 2.0.3 but we were running 2.0.2 - it did not support performAction false.  
> **username_1**: Great! Glad you got it fixed!  

---

