<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>

CC BY-SA 3.0
2015-10-07T08:39:58.460Z
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.
Lettie
CC BY-SA 3.0
2015-10-07T12:07:19.587Z
@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.)
Brandon Kelly
CC BY-SA 3.0
2015-10-07T14:13:43.450Z
cheers Brandon perfect.
Lettie
CC BY-SA 3.0
2015-10-06T19:32:02.930Z
2015-10-07T12:06:14.123Z
2015-10-07T12:06:14.123Z
Brandon Kelly
<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>

<plugin-development><users>
Extending user validation
# 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(), so restructure your code to this:
$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.
$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 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.  
> **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 Brandon perfect.  

---

