<p>Got it working using:</p>
<p><code>use yii\base\ModelEvent;</code></p>
<p><code>use craft\elements\User;</code></p>
<pre><code>Event::on(
User::class,
User::EVENT_BEFORE_VALIDATE,
function (ModelEvent $event) {

    // username format: john-doe
    $senderUsername = $this-&gt;request-&gt;getRequiredBodyParam('username');
    $senderEmail = $this-&gt;request-&gt;getRequiredBodyParam('email');
    $count = 1;

    if (!Craft::$app-&gt;getUsers()-&gt;getUserByUsernameOrEmail($senderEmail)){
        
        while(Craft::$app-&gt;getUsers()-&gt;getUserByUsernameOrEmail($senderUsername)){

            $name = explode(&quot;-&quot;, $senderUsername);
            $senderUsername = $name[0] . '-' . $name[1] . '-' . $count++;

        }
        
        $event-&gt;sender-&gt;username = $senderUsername;
    }
});
</code></pre>

CC BY-SA 4.0
2022-04-12T16:41:54.680Z
2022-04-19T15:11:06.997Z
2022-04-19T15:11:06.997Z
Mike Niland
<p>I have a front-end user registration form that generates a username automatically based on the first and last name inputs. This works as intended but on the off chance that two people have the same name, a check needs to be run on submit to see if the username already exists in the system. I'm currently utilizing <code>{{ actionInput('users/save-user') }}</code> to handle the form submit. I'm looking for a way to run a check before the action <code>users/save-user</code> returns the defualt duplicate username error. I've created a module to listen for the save event but it doesn't appear to be firing as intended. Within the module's <code>init()</code> function I'm running:</p>
<pre><code>Event::on(
 User::class,
 User::EVENT_BEFORE_SAVE,
 function (ModelEvent $event) {

  $loginName = $this-&gt;request-&gt;getRequiredBodyParam('username');
   
  if($user = Craft::$app-&gt;getUsers()-&gt;getUserByUsernameOrEmail($loginName)){

   // Change the username to username-(duplicateCount+1)
   // Create New User 

  } // else {

   // Create New User 

  // }
  }
);
</code></pre>
<p>using <code>use craft\elements\User;</code></p>
<p>Disclaimer: I'm a novice when it comes to modules/plugins/controllers so any advice would be greatly appreciated. Thanks!</p>

CC BY-SA 4.0
2022-04-12T07:24:15.637Z
The code looks ok at first glance – how are you checking if it's being executed? Try sticking a `\Craft::dd($loginName)` to see if the hook is actually firing. In this case it might just be down to a simple logic error.
MoritzLost
CC BY-SA 4.0
2022-04-12T14:17:14.450Z
To troubleshoot, I've been using ```die();``` within the event. The event fires correctly when a new user is created but fails to fire when loginName is a duplicate. Craft's default duplicate check appears to prevent ```EVENT_BEFORE_SAVE``` from firing.
Mike Niland
CC BY-SA 4.0
2022-04-12T14:30:30.927Z
I see, that makes sense. If there are validation errors, Craft won't try to save the user, so the hook won't fire. Try using `EVENT_BEFORE_VALIDATE` instead!
MoritzLost
CC BY-SA 4.0
2022-04-12T16:36:12.540Z
Somehow managed to miss that in the documentation, that worked. Thank you!
Mike Niland
<craft3><users><modules><registration><frontend-form>
Front-End user registration dynamic usernames - duplicate check
# Front-End user registration dynamic usernames - duplicate check

(Asked by: username_0 on 2022-04-11)

I have a front-end user registration form that generates a username automatically based on the first and last name inputs. This works as intended but on the off chance that two people have the same name, a check needs to be run on submit to see if the username already exists in the system. I'm currently utilizing {{ actionInput('users/save-user') }} to handle the form submit. I'm looking for a way to run a check before the action users/save-user returns the defualt duplicate username error. I've created a module to listen for the save event but it doesn't appear to be firing as intended. Within the module's init() function I'm running:
Event::on(
 User::class,
 User::EVENT_BEFORE_SAVE,
 function (ModelEvent $event) {

  $loginName = $this->request->getRequiredBodyParam('username');
   
  if($user = Craft::$app->getUsers()->getUserByUsernameOrEmail($loginName)){

   // Change the username to username-(duplicateCount+1)
   // Create New User 

  } // else {

   // Create New User 

  // }
  }
);

using use craft\elements\User;
Disclaimer: I'm a novice when it comes to modules/plugins/controllers so any advice would be greatly appreciated. Thanks!

**Tags:** craft3, users, modules, registration, frontend-form

> **Comments:**
> **username_1**: The code looks ok at first glance – how are you checking if it's being executed? Try sticking a `\Craft::dd($loginName)` to see if the hook is actually firing. In this case it might just be down to a simple logic error.  
> **username_0**: To troubleshoot, I've been using ```die();``` within the event. The event fires correctly when a new user is created but fails to fire when loginName is a duplicate. Craft's default duplicate check appears to prevent ```EVENT_BEFORE_SAVE``` from firing.  
> **username_1**: I see, that makes sense. If there are validation errors, Craft won't try to save the user, so the hook won't fire. Try using `EVENT_BEFORE_VALIDATE` instead!  
> **username_0**: Somehow managed to miss that in the documentation, that worked. Thank you!  

---

## 1 answers

### Answer by username_0 on 2022-04-12. Score: 1 | Accepted answer

Got it working using:
use yii\base\ModelEvent;
use craft\elements\User;
Event::on(
User::class,
User::EVENT_BEFORE_VALIDATE,
function (ModelEvent $event) {

    // username format: john-doe
    $senderUsername = $this->request->getRequiredBodyParam('username');
    $senderEmail = $this->request->getRequiredBodyParam('email');
    $count = 1;

    if (!Craft::$app->getUsers()->getUserByUsernameOrEmail($senderEmail)){
        
        while(Craft::$app->getUsers()->getUserByUsernameOrEmail($senderUsername)){

            $name = explode("-", $senderUsername);
            $senderUsername = $name[0] . '-' . $name[1] . '-' . $count++;

        }
        
        $event->sender->username = $senderUsername;
    }
});


---

