1

I'm trying to validate user's new password and confirm password in my custom plugin init().

Event::on(User::class, User::EVENT_BEFORE_SAVE, function(Event $event) {
    $formData = $_POST;
    $newPassword =  $formData['newPassword'];
    $confirmPassword =  $formData['confirmPassword'];

    if ($newPassword != $confirmPassword){
        Craft::$app->session->setError('Confirm password does not match new password');
    }
});

On the template:

{% set error = craft.app.session.getFlash('error') %}
{% if error|length > 0 %}
    {{error}}
{% endif %}

The problem with this is, it displays the message "Confirm password does not match new password" on frontend but the password gets changed into newPassword value. Is there a way to stop processing any further after returning error?

1 Answer 1

2

I believe what you're looking for is this:

if ($newPassword != $confirmPassword){
    Craft::$app->session->setError('Confirm password does not match new password');
    $event->isValid = false;
}

Setting isValid to false will stop further processing of that element (the user in this case).

0

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.