Answers.Body: <blockquote>
    <p>If you hit the submit button and don't fill out any fields. Then the only errors back are Email and Password (standard and set out by Craft). If you do enter an email and password then my validation code works and it errors again this time with the firstName and lastName errors. But these don't trigger on first submit when all fields are blank.</p>
  </blockquote>
  
  <p>This is a known issue (specifically with the <a href="https://craftcms.com/classreference/models/UserModel" rel="nofollow">UserModel</a> when saving a user) because "First Name" and "Last Name" aren't true fields in Craft, but more special case fields.</p>
  
  <p>When saving a user, the UserModel validation runs first and immediately bails when the validation error occurs before the <code>onBeforeSaveUser</code> event fires not giving any plugins a chance to participate.</p>
  
  <p>This will be fixed in the upcoming Craft 3 release.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-24T21:11:23.383Z
Answers.LastActivityDate: 2016-08-24T21:11:23.383Z
Answers.UserDisplayName: Brad Bell
Body: <p>I am trying to validate first name and last name on my user registration form. I have tested and the following works for existing users when updating their profile via the form. Both use the <a href="https://craftcms.com/docs/plugins/events-reference#users-onBeforeSaveUser" rel="nofollow"><code>onBeforeSaveUser</code></a> event.</p>
  
  <p>Any ideas why this wouldn't validate for registering users?</p>
  
  <pre><code>public function init()
  {
      craft()-&gt;on('users.onBeforeSaveUser', function(Event $event)
      {
          craft()-&gt;businessLogic_events-&gt;onBeforeSaveUser($event);
      });
  }
  
  public function onBeforeSaveUser(Event $event)
  {
      $user = $event-&gt;params['user'];
  
      if (empty($user-&gt;firstName))
      {
          $user-&gt;addError('firstName', Craft::t('Name cannot be empty.'));
          $event-&gt;performAction = false;
      }
  
      if (empty($user-&gt;lastName))
      {
          $user-&gt;addError('lastName', Craft::t('Last Name cannot be empty.'));
          $event-&gt;performAction = false;
      }
  }
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-08-24T18:10:07.247Z
Comments.Text: When testing a new user, does the user account actually get created?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-08-24T20:18:21.823Z
Comments.Text: Ok so I have done some testing. If you hit the submit button and don't fill out any fields. Then the only errors back are Email and Password (standard and set out by Craft). If you do enter an email and password then my validation code works and it errors again this time with the firstName and lastName errors. But these don't trigger on first submit when all fields are blank.
Comments.UserDisplayName: Terry Upton
Tags: <plugin-development><users>
Title: onBeforeSaveUser validation for existing user but not new user
ThreadText: # onBeforeSaveUser validation for existing user but not new user
  
  (Asked by: username_0 on 2016-08-24)
  
  I am trying to validate first name and last name on my user registration form. I have tested and the following works for existing users when updating their profile via the form. Both use the [`onBeforeSaveUser`](https://craftcms.com/docs/plugins/events-reference#users-onBeforeSaveUser) event.
  
  Any ideas why this wouldn't validate for registering users?
  
  ```
  public function init()
  {
      craft()->on('users.onBeforeSaveUser', function(Event $event)
      {
          craft()->businessLogic_events->onBeforeSaveUser($event);
      });
  }
  
  public function onBeforeSaveUser(Event $event)
  {
      $user = $event->params['user'];
  
      if (empty($user->firstName))
      {
          $user->addError('firstName', Craft::t('Name cannot be empty.'));
          $event->performAction = false;
      }
  
      if (empty($user->lastName))
      {
          $user->addError('lastName', Craft::t('Last Name cannot be empty.'));
          $event->performAction = false;
      }
  }
  
  ```
  
  **Tags:** plugin-development, users
  
  > **Comments:**
  > **username_1**: When testing a new user, does the user account actually get created?  
  > **username_0**: Ok so I have done some testing. If you hit the submit button and don't fill out any fields. Then the only errors back are Email and Password (standard and set out by Craft). If you do enter an email and password then my validation code works and it errors again this time with the firstName and lastName errors. But these don't trigger on first submit when all fields are blank.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-08-24. Score: 2 | Accepted answer
  
  > If you hit the submit button and don't fill out any fields. Then the only errors back are Email and Password (standard and set out by Craft). If you do enter an email and password then my validation code works and it errors again this time with the firstName and lastName errors. But these don't trigger on first submit when all fields are blank.
  
  This is a known issue (specifically with the [UserModel](https://craftcms.com/classreference/models/UserModel) when saving a user) because "First Name" and "Last Name" aren't true fields in Craft, but more special case fields.
  
  When saving a user, the UserModel validation runs first and immediately bails when the validation error occurs before the `onBeforeSaveUser` event fires not giving any plugins a chance to participate.
  
  This will be fixed in the upcoming Craft 3 release.
  
  
  ---
  
  