<p>Take a look at Craft's own <a href="https://github.com/craftcms/cms/blob/develop/src/controllers/UsersController.php#L919-L1212" rel="nofollow noreferrer">UsersController->actionSaveUser()</a>.</p>

<p>You're probably looking for something closer to this:</p>

<pre><code>$user = new craft\elements\User();

$user-&gt;firstName = $firstName;
$user-&gt;lastName = $lastName;
$user-&gt;username = $userName;
$user-&gt;email = $email;
$user-&gt;password = $password;

if ($user-&gt;validate(null, false)) {
    Craft::$app-&gt;getElements()-&gt;saveElement($user, false);
}
</code></pre>

CC BY-SA 4.0
2019-05-14T12:29:15.377Z
I've found that if you actually want to set the password, you need to use `$user->newPassword = $password;` Other than that, this works exactly as expected, thanks.
jymbob
CC BY-SA 4.0
2022-03-22T17:18:11.373Z
Thanks for this. What is `$user->validate(null, false)`?
iamkeir
CC BY-SA 4.0
2019-03-29T03:01:10.980Z
2019-03-29T03:01:10.980Z
Brad Bell
<p>Having difficulties creating a new user in a custom controller, here's my implementation:</p>

<p><code>use craft\records\User;</code></p>

<pre><code>$user = new User;
$user-&gt;firstName = $userData['firstName'];
$user-&gt;lastName = $userData['lastName'];
$user-&gt;username = $userData['loginName'];
$user-&gt;email = $userData['loginName'];
$user-&gt;password = $userData['password'];

$response = $user-&gt;insert();
</code></pre>

<p>Response:</p>

<pre><code>SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value
The SQL being executed was: INSERT INTO `craft_users` (`firstName`, `lastName`, `username`, `email`, `password`, `dateCreated`, `uid`, `dateUpdated`) VALUES ('Tristan', 'Gemus', 'registertest@gmail.com', 'registertest@gmail.com', 'password', '2019-03-28 15:53:10', '85468ad6-63cc-499d-baf4-32aaaf55bfb2', '2019-03-28 15:53:10')
</code></pre>

<p>The response returns false. Thoughts?</p>

<craft3><yii>
Craft CMS V3 Create New User
# Craft CMS V3 Create New User

(Asked by: username_0 on 2019-03-28)

Having difficulties creating a new user in a custom controller, here's my implementation:
use craft\records\User;
$user = new User;
$user->firstName = $userData['firstName'];
$user->lastName = $userData['lastName'];
$user->username = $userData['loginName'];
$user->email = $userData['loginName'];
$user->password = $userData['password'];

$response = $user->insert();

Response:
SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value
The SQL being executed was: INSERT INTO `craft_users` (`firstName`, `lastName`, `username`, `email`, `password`, `dateCreated`, `uid`, `dateUpdated`) VALUES ('Tristan', 'Gemus', 'registertest@gmail.com', 'registertest@gmail.com', 'password', '2019-03-28 15:53:10', '85468ad6-63cc-499d-baf4-32aaaf55bfb2', '2019-03-28 15:53:10')

The response returns false. Thoughts?

**Tags:** craft3, yii


---

## 1 answers

### Answer by username_1 on 2019-03-29. Score: 4

Take a look at Craft's own UsersController->actionSaveUser().
You're probably looking for something closer to this:
$user = new craft\elements\User();

$user->firstName = $firstName;
$user->lastName = $lastName;
$user->username = $userName;
$user->email = $email;
$user->password = $password;

if ($user->validate(null, false)) {
    Craft::$app->getElements()->saveElement($user, false);
}

> **Comments:**
> **username_2**: I've found that if you actually want to set the password, you need to use `$user->newPassword = $password;` Other than that, this works exactly as expected, thanks.  
> **username_3**: Thanks for this. What is `$user->validate(null, false)`?  

---

