I'm trying to add some rules for users signing up. The first two (fullName
and email
) work fine. However, the password rules are triggered no matter what. Even if the passwords match and are over 8 characters, it still fails.
Event::on(User::class, Model::EVENT_BEFORE_VALIDATE, static function (Event $event) {
// If a user is already logged in we are not on the public registration page
if (Craft::$app->getUser()->getIdentity()) {
return;
}
$user = $event->sender;
// Do the custom validation
if (!$user->fullName) {
$user->addError('fullName', Craft::t('yii', 'Please enter your name.'));
$event->isValid = false;
}
if (!$user->email) {
$user->addError('email', Craft::t('yii', 'Please enter a valid email.'));
$event->isValid = false;
}
if (mb_strlen($user->password) < 8) {
$user->addError('password', Craft::t('yii', 'Password must be more than 8 characters.'));
$event->isValid = false;
}
if ($user->password != $user-> confirmPassword) {
$user->addError('confirmPassword', Craft::t('yii', 'Passwords do not match'));
$event->isValid = false;
}
});
<label for="password">Password</label>
<input id="password" name="password" type="password">
{{ user ? forms.errorsList(user.getErrors('password')) }}
<label for="confirmPassword">Confirm Password</label>
<input id="confirmPassword" name="confirmPassword" type="password">
{{ user ? forms.errorsList(user.getErrors('confirmPassword')) }}
</div>
Can anyone spot what I'm missing?