<p>Events are generally not capable of having an effect on the calling code; they’re just notifications.</p>

<p>You would probably be better off disabling public user registration (Settings > Users > Settings), and creating a custom controller that registers users instead. Your controller would be the one to determine if there are still any slots left, and if so, call <a href="http://buildwithcraft.com/classreference/services/UsersService#saveUser-detail" rel="nofollow">craft()->users->saveUser()</a> itself.</p>

CC BY-SA 3.0
2014-10-08T16:17:48.210Z
I'm in a similar situation of needing to do additional validation onBeforeSaveEntry and would like to stop the action gracefully. Is this something that you have considered adding? Or would this be considered bad form in general to allow events to interfere with an action?
Douglas McDonald
CC BY-SA 3.0
2014-10-08T16:23:24.223Z
@DouglasMcDonald I’m generally of the opinion that events should just be notifications. If you want to add additional validation, that can be handled with a custom controller.
Brandon Kelly
CC BY-SA 3.0
2014-10-09T04:09:39.767Z
I agree in principle 100%. Just trying to figure out a workaround? Is it possible to re-route default actions (i.e. `action/saveEntry`) to a custom plugin controller using registerCPRoutes? If so, that would definitely solve the problem.
Douglas McDonald
CC BY-SA 3.0
2014-10-09T11:19:14.513Z
@DouglasMcDonald Sorry I didn’t realize you meant within the CP. That may be a case where it would make sense to give the event a little more power. I’ll put it on the list to think about.
Brandon Kelly
CC BY-SA 3.0
2014-10-08T15:57:34.307Z
2014-10-08T15:57:34.307Z
Brandon Kelly
<p>I'm looking to add some additional functionality to the user registration process. Basically, we will be creating "access codes" that can be used N number of times during registration. Once N users have signed up using that code, the plugin that I'm writing will prevent additional users from registering on the site.</p>

<p>Right now, I'm having trouble determining how to add errors to the <a href="http://buildwithcraft.com/docs/plugins/events-reference#users-onBeforeSaveUser" rel="nofollow"><code>users.onBeforeSaveUser</code></a> event. My plugin checks availability of the access code that was submitted during the registration process, and I'd like to prevent Craft from saving the user if the code isn't valid/enabled. Here's what the code looks like:</p>

<pre><code>&lt;?php

namespace Craft;

class GatekeeperPlugin extends BasePlugin
{

  ...

  function init()
  {

    craft()-&gt;on('users.onBeforeSaveUser', function(Event $event) {

      // The user that's about to be saved
      $user = $event-&gt;params['user'];

      // Determine if an access code was submitted
      $accessCodeUsed = isset($user-&gt;contentFromPost['accessCodeUsed']) ? $user-&gt;contentFromPost['accessCodeUsed'] : false ;

      // If there was an access code submitted...
      if ($accessCodeUsed)
      {

        // Look for it in the database
        $queryAccessCode = craft()-&gt;db-&gt;createCommand()
          -&gt;select('id, field_code, field_limit, field_timesUsed')
          -&gt;from('content')
          -&gt;where(array('field_code' =&gt; $accessCodeUsed))
          -&gt;limit('1')
          -&gt;queryRow();

        // If the code was found...
        if($queryAccessCode)
        {

          ...Do some stuff

        }

        // If the code wasn't found...
        else
        {

          // Prevent Craft from saving the user
          // Add error "Sorry, that code is not valid."


        }

      }

    });

  }

}
</code></pre>

<p>It appears that error messages can only be added to models, but I'm working with an event here. The event has a user model within it, but trying to add errors to it (e.g. <code>$event-&gt;params['user']-&gt;addErrors([array of errors])</code>) hasn't worked for me. Any pointers, here?</p>

<plugin-development><error-message>
Add errors to an event via plugin
# Add errors to an event via plugin

(Asked by: username_0 on 2014-10-08)

I'm looking to add some additional functionality to the user registration process. Basically, we will be creating "access codes" that can be used N number of times during registration. Once N users have signed up using that code, the plugin that I'm writing will prevent additional users from registering on the site.
Right now, I'm having trouble determining how to add errors to the users.onBeforeSaveUser event. My plugin checks availability of the access code that was submitted during the registration process, and I'd like to prevent Craft from saving the user if the code isn't valid/enabled. Here's what the code looks like:
on('users.onBeforeSaveUser', function(Event $event) {

      // The user that's about to be saved
      $user = $event->params['user'];

      // Determine if an access code was submitted
      $accessCodeUsed = isset($user->contentFromPost['accessCodeUsed']) ? $user->contentFromPost['accessCodeUsed'] : false ;

      // If there was an access code submitted...
      if ($accessCodeUsed)
      {

        // Look for it in the database
        $queryAccessCode = craft()->db->createCommand()
          ->select('id, field_code, field_limit, field_timesUsed')
          ->from('content')
          ->where(array('field_code' => $accessCodeUsed))
          ->limit('1')
          ->queryRow();

        // If the code was found...
        if($queryAccessCode)
        {

          ...Do some stuff

        }

        // If the code wasn't found...
        else
        {

          // Prevent Craft from saving the user
          // Add error "Sorry, that code is not valid."


        }

      }

    });

  }

}

It appears that error messages can only be added to models, but I'm working with an event here. The event has a user model within it, but trying to add errors to it (e.g. $event->params['user']->addErrors([array of errors])) hasn't worked for me. Any pointers, here?

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


---

## 1 answers

### Answer by username_1 on 2014-10-08. Score: 2

Events are generally not capable of having an effect on the calling code; they’re just notifications.

You would probably be better off disabling public user registration (Settings > Users > Settings), and creating a custom controller that registers users instead. Your controller would be the one to determine if there are still any slots left, and if so, call [craft()->users->saveUser()](http://buildwithcraft.com/classreference/services/UsersService#saveUser-detail) itself.

> **Comments:**
> **username_2**: I'm in a similar situation of needing to do additional validation onBeforeSaveEntry and would like to stop the action gracefully. Is this something that you have considered adding? Or would this be considered bad form in general to allow events to interfere with an action?  
> **username_1**: @DouglasMcDonald I’m generally of the opinion that events should just be notifications. If you want to add additional validation, that can be handled with a custom controller.  
> **username_2**: I agree in principle 100%. Just trying to figure out a workaround? Is it possible to re-route default actions (i.e. `action/saveEntry`) to a custom plugin controller using registerCPRoutes? If so, that would definitely solve the problem.  
> **username_1**: @DouglasMcDonald Sorry I didn’t realize you meant within the CP. That may be a case where it would make sense to give the event a little more power. I’ll put it on the list to think about.  

---

