<p>I believe what you're looking for is this:</p>

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

<p>Setting <code>isValid</code> to false will stop further processing of that element (the user in this case).</p>

CC BY-SA 4.0
2018-10-16T00:32:31.930Z
2018-10-16T00:32:31.930Z
Brad Bell
<p>I'm trying to validate user's new password and confirm password in my custom plugin <code>init()</code>.</p>

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

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

<p>On the template:</p>

<pre><code>{% set error = craft.app.session.getFlash('error') %}
{% if error|length &gt; 0 %}
    {{error}}
{% endif %}
</code></pre>

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

<plugin-development><craft3><users><password>
Craft 3 validate new password in change password
# Craft 3 validate new password in change password

(Asked by: username_0 on 2018-10-16)

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?

**Tags:** plugin-development, craft3, users, password

---

## 1 answers

### Answer by username_1 on 2018-10-16. Score: 2 | Accepted answer

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).


---

