<p>This functionality or behavior is a bit confusing. The Elements Service <a href="https://github.com/craftcms/cms/blob/master/src/services/Elements.php#L1877-L1882" rel="nofollow noreferrer">has its own Events</a> for saving <em>Elements</em>, specifically... but because ultimately Elements are just Yii Models, <a href="https://github.com/craftcms/cms/blob/master/src/base/Element.php#L2218-L2224" rel="nofollow noreferrer">they have a discrete <code>EVENT_BEFORE_SAVE</code> event</a> that uses a ModelEvent, which <a href="https://www.yiiframework.com/doc/api/2.0/yii-base-modelevent#$isValid-detail" rel="nofollow noreferrer">has the <code>isValid</code> property</a>.</p>

<p>This is the point in the life-cycle where saving the Element can be prevented. The <code>beforeSave</code> method is called on the Element being saved (usually overridden in the actual Element implementation, but <a href="https://github.com/craftcms/cms/blob/master/src/elements/Entry.php#L1217" rel="nofollow noreferrer">called + returned at the end of the subclass's method</a>, like <code>parent::beforeSave($isNew)</code>), and the resulting boolean (<code>$event-&gt;isValid</code>) <a href="https://github.com/craftcms/cms/blob/master/src/services/Elements.php#L1884-L1886" rel="nofollow noreferrer">determines whether or not the saving continues</a>.</p>

<p>So, in short: You can listen directly to the Entry's <code>EVENT_BEFORE_SAVE</code> event to prevent it from saving:</p>

<pre class="lang-php prettyprint-override"><code>use craft\elements\Entry;
use craft\events\ModelEvent;

use yii\base\Event;

Event::on(
    Entry::class,
    Entry::EVENT_BEFORE_SAVE,
    function(ModelEvent $event) {
            $entry = $event-&gt;sender;
            if ($entry-&gt;sectionId == 5) {
                $event-&gt;isValid = false;
            }
        }
    });
</code></pre>

CC BY-SA 4.0
2019-12-12T21:01:32.483Z
2019-12-13T09:21:37.020Z
2019-12-13T09:21:37.020Z
August Miller
<p>I want to do some server side checks to see if the entry should be saved or not.</p>

<p>In my plugin I have something like that</p>

<pre><code>Event::on(
    Elements::class,
    Elements::EVENT_BEFORE_SAVE_ELEMENT,
    function(ElementEvent $event) {
        if ($event-&gt;element instanceof \craft\elements\Entry) {
            $entry = $event-&gt;element;
            if ( $entry-&gt;sectionId == 5 ) {
                $entry-&gt;enabled = 0;
            }
        }
    });
</code></pre>

<p>This works but what I want is for the entry to not be saved rather then be disabled. Something I could achive on the user model with:</p>

<pre><code>$event-&gt;isValid = false;
</code></pre>

<p>Is there a way to withhold the entry from saving? The only other solution I can think if is to make the tests in the AFTER event and delete it if not passing the condition. </p>

CC BY-SA 4.0
2019-12-12T14:19:16.223Z
Just a heads up in case no solution is found: Deleting an element after it's saved would seriously screw up your database's indexes (auto-increment columns mostly),  though you wouldn't really notice it since craft would be managing all things SQL-related.
Campiotti
<plugin-development><craft3><events>
Choose not to save an element on EVENT_BEFORE_SAVE_ELEMENT
# Choose not to save an element on EVENT_BEFORE_SAVE_ELEMENT

(Asked by: username_0 on 2019-12-12)

I want to do some server side checks to see if the entry should be saved or not.
In my plugin I have something like that
Event::on(
    Elements::class,
    Elements::EVENT_BEFORE_SAVE_ELEMENT,
    function(ElementEvent $event) {
        if ($event->element instanceof \craft\elements\Entry) {
            $entry = $event->element;
            if ( $entry->sectionId == 5 ) {
                $entry->enabled = 0;
            }
        }
    });

This works but what I want is for the entry to not be saved rather then be disabled. Something I could achive on the user model with:
$event->isValid = false;

Is there a way to withhold the entry from saving? The only other solution I can think if is to make the tests in the AFTER event and delete it if not passing the condition.

**Tags:** plugin-development, craft3, events

> **Comments:**
> **username_1**: Just a heads up in case no solution is found: Deleting an element after it's saved would seriously screw up your database's indexes (auto-increment columns mostly),  though you wouldn't really notice it since craft would be managing all things SQL-related.  

---

## 1 answers

### Answer by username_2 on 2019-12-12. Score: 3 | Accepted answer

This functionality or behavior is a bit confusing. The Elements Service has its own Events for saving Elements, specifically... but because ultimately Elements are just Yii Models, they have a discrete EVENT_BEFORE_SAVE event that uses a ModelEvent, which has the isValid property.
This is the point in the life-cycle where saving the Element can be prevented. The beforeSave method is called on the Element being saved (usually overridden in the actual Element implementation, but called + returned at the end of the subclass's method, like parent::beforeSave($isNew)), and the resulting boolean ($event->isValid) determines whether or not the saving continues.
So, in short: You can listen directly to the Entry's EVENT_BEFORE_SAVE event to prevent it from saving:
use craft\elements\Entry;
use craft\events\ModelEvent;

use yii\base\Event;

Event::on(
    Entry::class,
    Entry::EVENT_BEFORE_SAVE,
    function(ModelEvent $event) {
            $entry = $event->sender;
            if ($entry->sectionId == 5) {
                $event->isValid = false;
            }
        }
    });


---

