Skip to main content
added 3 characters in body
Source Link
Brad Bell
  • 67.6k
  • 6
  • 75
  • 145

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

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.

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.

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.

Source Link
Terry Upton
  • 1.9k
  • 11
  • 28

Password validation for resetting password

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.