3

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

1 Answer 1

3

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
    ));
}
3
  • 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
    Oct 7, 2015 at 8:39
  • @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.) Oct 7, 2015 at 12:07
  • cheers Brandon perfect.
    – Lettie
    Oct 7, 2015 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.