Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-14T12:29:15.377Z
Answers.Text: 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.
Answers.UserDisplayName: jymbob
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-03-22T17:18:11.373Z
Answers.Text: Thanks for this. What is `$user->validate(null, false)`?
Answers.UserDisplayName: iamkeir
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-29T03:01:10.980Z
Answers.LastActivityDate: 2019-03-29T03:01:10.980Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Tags: <craft3><yii>
Title: Craft CMS V3 Create New User
ThreadText: # 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()](https://github.com/craftcms/cms/blob/develop/src/controllers/UsersController.php#L919-L1212).
  
  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)`?  
  
  ---
  
  