<p><a href="https://i.sstatic.net/Milin.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Milin.png" alt="Screenshot of PHPStorm code showing the name of the second parameter of the Save Element function is Run Validation" /></a></p>
<p>You're passing <code>false</code> as the second parameter of your saveElement call... that parameter means you're skipping the built-in validation which should catch this error for you. If you get rid of that you should then receive a warning in the logs when processing existing users, but it shouldn't halt execution of the script.</p>

CC BY-SA 4.0
2020-12-07T15:57:20.473Z
2020-12-07T15:57:20.473Z
James Smith
<p>I am adding new users inside of the Craft controller as part of the frontend user import script.
This is the code:</p>
<pre><code>$user = new User();
$user-&gt;pending = true;
$user-&gt;username = $email;
$user-&gt;firstName = $firstName;
$user-&gt;lastName = $lastName;
$user-&gt;email = $email;
$user-&gt;passwordResetRequired = false;
$user-&gt;validate(null, false);
Craft::$app-&gt;getElements()-&gt;saveElement($user, false);
</code></pre>
<p>The problem is that if I try to import two users with the same email addresses they just get imported and that's obviously something I don't want to happen. How can I prevent that and silently skip the users with existing email addresses.</p>
<p>I could check the user's existence by querying it in the database but I would like to avoid any unnecessary db queries because there will be thousands of users imported at the same time. That validation line looks promising but I'm not finding any useful instructions in the docs.</p>

<craft3><users><controller>
Prevent adding users with same email addresses
# Prevent adding users with same email addresses

(Asked by: username_0 on 2020-12-07)

I am adding new users inside of the Craft controller as part of the frontend user import script.
This is the code:
$user = new User();
$user->pending = true;
$user->username = $email;
$user->firstName = $firstName;
$user->lastName = $lastName;
$user->email = $email;
$user->passwordResetRequired = false;
$user->validate(null, false);
Craft::$app->getElements()->saveElement($user, false);

The problem is that if I try to import two users with the same email addresses they just get imported and that's obviously something I don't want to happen. How can I prevent that and silently skip the users with existing email addresses.
I could check the user's existence by querying it in the database but I would like to avoid any unnecessary db queries because there will be thousands of users imported at the same time. That validation line looks promising but I'm not finding any useful instructions in the docs.

**Tags:** craft3, users, controller


---

## 1 answers

### Answer by username_1 on 2020-12-07. Score: 3 | Accepted answer

[![Screenshot of PHPStorm code showing the name of the second parameter of the Save Element function is Run Validation](https://i.sstatic.net/Milin.png)](https://i.sstatic.net/Milin.png)

You're passing `false` as the second parameter of your saveElement call... that parameter means you're skipping the built-in validation which should catch this error for you. If you get rid of that you should then receive a warning in the logs when processing existing users, but it shouldn't halt execution of the script.


---

