1

I have the following code to further validate passwords when creating/registering an account which is working fine.

However, when trying to use this same validation for resetting a password I am not getting any errors back?

Can anyone point me in the right direction...?

public function onBeforeSetPassword(Event $event)
{
    $user = $event->params['user'];
    $password = $event->params['password'];

    if (mb_strlen($password) < 8)
    {
        $user->addError('password', 'Sorry but your password must be at least 8 characters.');
        $event->performAction = false;
    }

    if (!preg_match('/(\d.*[A-Z]|[A-Z].*\d)/', $password))
    {
        $user->addError('password', 'Sorry but your password must contain at least one uppercase letter and one number.');
        $event->performAction = false;
    }
}

On the template:

{% if account is defined and account.getErrors('password') %}
    {% include '_includes/formError' with { error : account.getErrors('password')|join('<br>') } %}
{% endif %}

Thanks.

3
  • Is this in the context of a front-end registered user or a CP registered one? Also, if you Craft::log() in there, is your method even being hit in the reset password scenario?
    – Brad Bell
    Commented Oct 2, 2016 at 17:02
  • FWIW, I checked and the onBeforeSetPassword event does seem to fire when passwords are reset. You can confirm this yourself by adding MyPluginHandlePlugin::log("Setting password..."); to your event handler (obviously, replace MyPluginHandle with the real handle for your plugin), and then open up your plugin's log file at /craft/storage/runtime/logs/mypluginhandle.log, after resetting a password. If the event does indeed fire, there's something else amiss with your code somewhere. Commented Oct 2, 2016 at 17:02
  • @BradBell this is on a front end user (actually customers for commerce) form. I will try what Mats has suggest to see if firing. Commented Oct 4, 2016 at 6:59

1 Answer 1

2

I wasn't validating againts the field newPassword, only validating against password. Hence why I wasn't getting any feedback. Duplicating the validation for newPassword seems to have resolved this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.