Answers.Body: <p>Most user validation happens in <a href="http://buildwithcraft.com/classreference/services/UsersService#saveUser-detail" rel="nofollow">UsersService::saveUser()</a>, so restructure your code to this:</p>
  
  
  
  <pre class="lang-php prettyprint-override"><code>$success = craft()-&gt;users-&gt;saveUser($newUser);
  
  if (!$success)
  {
      return $newUser-&gt;getErrors();
  }
  </code></pre>
  
  <p>If this is happening in a controller action, you’ll want to make the UserModel available to the original template, so it can access and display those errors.</p>
  
  <pre class="lang-php prettyprint-override"><code>$success = craft()-&gt;users-&gt;saveUser($newUser);
  
  if ($success)
  {
      // Redirect to whatever URL the 'redirect' param specified
      $this-&gt;redirectToPostedUrl($newUser);
  }
  else
  {
      // Make the UserModel available to the original template
      // so it can access the submitted values and validation errors
      craft()-&gt;urlManager-&gt;setRouteVariables(array(
          'account' =&gt; $newUser
      ));
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-07T08:39:58.460Z
Answers.Text: thanks Brandon that does now return the full error list when I do a print_r. However I'm still not seeing any of the errors being displayed on the register template. As I said I'm using the basic supplied one which is checking for errors with the macro.
  
      {% from _self import errorList %}
  
  And then looking at:
  
      {% if account is defined %}
          {{ errorList(account.getErrors('username')) }}
      {% endif %}
  
  For each error. Can you shed any light on why nothing is showing? If I switch back to using just saveUser as the action the messages appear! Cheers.
Answers.UserDisplayName: Lettie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-07T12:07:19.587Z
Answers.Text: @Lettie Gotcha, didn’t realize this was happening directly in a controller action. Updated my answer with a code example for that case. (This is a pretty similar approach to how most internal Craft controllers will handle requests like this.)
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-07T14:13:43.450Z
Answers.Text: cheers Brandon perfect.
Answers.UserDisplayName: Lettie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-06T19:32:02.930Z
Answers.LastActivityDate: 2015-10-07T12:06:14.123Z
Answers.LastEditDate: 2015-10-07T12:06:14.123Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>Hi I've created a plugin that has an action in the controller that looks at a user registration form when submitted and if the username is empty creates a unique username for the user. This works fine and saves the user with the random name or with their chosen name if a username is entered. My problem is I am not getting any errors returned to the form or a success message when the user is registered. Validation appears to be happening as the user is not being registered if errors are found. Although the only error I see printed is invalid email if entered incorrectly, if the password is missing there is no error returned but the user is not registered.</p>
  
  <p>My code to add the model is as follows. Can anyone point me in the right direction so that the errors are returned. The template is the standard registration form from the craft documentation.</p>
  
  <pre><code>$newUser = new UserModel();
  $newUser-&gt;username = $username;
  $newUser-&gt;email = $email;
  $newUser-&gt;newPassword = $password;
  
  if ($newUser-&gt;validate())
  {
      craft()-&gt;users-&gt;saveUser($newUser);
  }
  else
  {
      return $newUser-&gt;getErrors();
  }
  </code></pre>
  
Tags: <plugin-development><users>
Title: Extending user validation
ThreadText: # Extending user validation
  
  (Asked by: username_0 on 2015-10-06)
  
  Hi I've created a plugin that has an action in the controller that looks at a user registration form when submitted and if the username is empty creates a unique username for the user. This works fine and saves the user with the random name or with their chosen name if a username is entered. My problem is I am not getting any errors returned to the form or a success message when the user is registered. Validation appears to be happening as the user is not being registered if errors are found. Although the only error I see printed is invalid email if entered incorrectly, if the password is missing there is no error returned but the user is not registered.
  
  My code to add the model is as follows. Can anyone point me in the right direction so that the errors are returned. The template is the standard registration form from the craft documentation.
  
  ```
  $newUser = new UserModel();
  $newUser->username = $username;
  $newUser->email = $email;
  $newUser->newPassword = $password;
  
  if ($newUser->validate())
  {
      craft()->users->saveUser($newUser);
  }
  else
  {
      return $newUser->getErrors();
  }
  
  ```
  
  **Tags:** plugin-development, users
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-10-06. Score: 3 | Accepted answer
  
  Most user validation happens in [UsersService::saveUser()](http://buildwithcraft.com/classreference/services/UsersService#saveUser-detail), so restructure your code to this:
  
  ```php
  $success = craft()->users->saveUser($newUser);
  
  if (!$success)
  {
      return $newUser->getErrors();
  }
  
  ```
  
  If this is happening in a controller action, you’ll want to make the UserModel available to the original template, so it can access and display those errors.
  
  ```php
  $success = craft()->users->saveUser($newUser);
  
  if ($success)
  {
      // Redirect to whatever URL the 'redirect' param specified
      $this->redirectToPostedUrl($newUser);
  }
  else
  {
      // Make the UserModel available to the original template
      // so it can access the submitted values and validation errors
      craft()->urlManager->setRouteVariables(array(
          'account' => $newUser
      ));
  }
  
  ```
  
  > **Comments:**
  > **username_0**: thanks username_1 that does now return the full error list when I do a print_r. However I'm still not seeing any of the errors being displayed on the register template. As I said I'm using the basic supplied one which is checking for errors with the macro.      {% from _self import errorList %}  And then looking at:      {% if account is defined %}         {{ errorList(account.getErrors('username')) }}     {% endif %}  For each error. Can you shed any light on why nothing is showing? If I switch back to using just saveUser as the action the messages appear! Cheers.  
  > **username_1**: @username_0 Gotcha, didn’t realize this was happening directly in a controller action. Updated my answer with a code example for that case. (This is a pretty similar approach to how most internal Craft controllers will handle requests like this.)  
  > **username_0**: cheers username_1 perfect.  
  
  ---
  
  