Craft 3: Preventing Element from Saving in Plugin I had a simple Password Confirmation validation function in Craft 2. In porting the function to Craft 3, I'm struggling to figure out how to handle the same thing. In Craft 2, all events were consistently handled by setting $event-&gt;performAction = false but in Craft 3, only a subset of events have a similar isValid option.

Here is my previous Craft 2 version, triggered on onBeforeSaveUser...

public function confirmPassword($event)
{
    // Only do anything if it is a front end submission
    // Only validates against passwordConfirm if field is present
    if (craft()-&gt;request-&gt;isSiteRequest()) {
        $password = Craft::$app-&gt;request-&gt;post('password');
        $passwordConfirm = Craft::$app-&gt;request-&gt;post('passwordConfirm');
        if (isset($passwordConfirm) &amp;&amp; strcmp($password, $passwordConfirm) !== 0) {
            $event-&gt;user-&gt;addErrors(array('passwordConfirm' =&gt; Craft::t('Passwords do not match.')));
            $event-&gt;performAction = false;
        }
    }
}


In my attempt to port to Craft 3, right now, I'm listening for EVENT_BEFORE_SAVE_ELEMENT and then doing...

public function confirmPassword(ElementEvent $event)
{
    // Only do anything if it is a front end submission
    // Only validates against passwordConfirm if field is present

    if (Craft::$app-&gt;request-&gt;isSiteRequest) {

        $password = Craft::$app-&gt;request-&gt;getBodyParam('password');
        $passwordConfirm = Craft::$app-&gt;request-&gt;getBodyParam('passwordConfirm');

        if (isset($passwordConfirm) &amp;&amp; strcmp($password, $passwordConfirm) !== 0) {

            $event-&gt;element-&gt;addErrors([
                'password' =&gt; 'Passwords do not match.'
            ]);

            // WHAT DO I DO HERE TO PREVENT USER ELEMENT FROM SAVING?

        }
    }
}


When I print out $event-&gt;element-&gt;errors, it correctly shows the errors. But the element is still saved. Am I missing a step here? Or is there a better way to handle this simple/custom validation?
The event fired by element service is not cancellable, as that is a bit too broad to be controlled like that. Instead, you should be listening to the EVENT_BEFORE_SAVE on the User element as that supports the isValid property.

use craft\events\ModelEvent;
use craft\elements\User;
use yii\base\Event;

Event::on(User::class, User::EVENT_BEFORE_SAVE, function(ModelEvent $event) {
    $event-&gt;sender-&gt;addErrors([
        'password' =&gt; 'Passwords do not match.'
    ]);
    $event-&gt;isValid = false;
});