<p>Try to use EVENT_BEFORE_SAVE instead EVENT_AFTER_SAVE, because if you use after save event, the data already stored to the database.</p>
<p>But since your purpose is to validate the entry I would like to suggest to use EVENT_DEFINE_RULES, so it should be something like this :</p>
<pre><code>Event::on(
            Entry::class,
            Model::EVENT_DEFINE_RULES,
            function(DefineRulesEvent $event) {
                $entry = $event-&gt;sender;
                 $event-&gt;rules[] = [['expiryDate'], 'required', 'when' =&gt; function($entry): bool {
        return $entry-&gt;type-&gt;id === 19;
    }];
            });
</code></pre>
<p>or</p>
<pre><code>Event::on(
                Entry::class,
                Model::EVENT_DEFINE_RULES,
                function(DefineRulesEvent $event) {
                    $entry = $event-&gt;sender;
                    if ($entry-&gt;type-&gt;id === 19) {
                       $event-&gt;rules[] = [['expiryDate'], 'required'];
                    }
                });
</code></pre>

CC BY-SA 4.0
2022-10-14T05:13:11.367Z
Ah very interesting, thank you! That's exactly what the issue was. So this was just the first step in getting a module to work, where I want to apply the date from another field to the expiry date - I'm thinking in that case `EVENT_BEFORE_SAVE` is probably what I need, but thank you for the additional info!
supazu
CC BY-SA 4.0
2022-10-14T04:20:03.683Z
2022-10-14T04:20:03.683Z
aodihis
<p>I'm trying to setup a module to require the Expiry Date on a specific Entry Type, borrowing from <a href="https://craftcms.stackexchange.com/questions/9320/is-there-a-way-to-require-the-expiration-date">this question</a>.</p>
<p>Can anyone help me learn why my code is wrong?</p>
<pre><code>Event::on(Element::class, Element::EVENT_AFTER_SAVE, function (ModelEvent $event) {
  if ($event-&gt;sender instanceof craft\elements\Entry) {
    $entry = $event-&gt;sender;
    if ($entry-&gt;type-&gt;id == '19' &amp;&amp; !$entry-&gt;expiryDate) {
      $entry-&gt;addError('expiryDate', 'Expiration date cannot be blank.');
      $event-&gt;isValid = false;
    }
  }
});
</code></pre>

<modules>
Require Expiry Date
# Require Expiry Date

(Asked by: username_0 on 2022-10-14)

I'm trying to setup a module to require the Expiry Date on a specific Entry Type, borrowing from this question.
Can anyone help me learn why my code is wrong?
Event::on(Element::class, Element::EVENT_AFTER_SAVE, function (ModelEvent $event) {
  if ($event->sender instanceof craft\elements\Entry) {
    $entry = $event->sender;
    if ($entry->type->id == '19' && !$entry->expiryDate) {
      $entry->addError('expiryDate', 'Expiration date cannot be blank.');
      $event->isValid = false;
    }
  }
});

**Tags:** modules


---

## 1 answers

### Answer by username_1 on 2022-10-14. Score: 2 | Accepted answer

Try to use EVENT_BEFORE_SAVE instead EVENT_AFTER_SAVE, because if you use after save event, the data already stored to the database.
But since your purpose is to validate the entry I would like to suggest to use EVENT_DEFINE_RULES, so it should be something like this :
Event::on(
            Entry::class,
            Model::EVENT_DEFINE_RULES,
            function(DefineRulesEvent $event) {
                $entry = $event->sender;
                 $event->rules[] = [['expiryDate'], 'required', 'when' => function($entry): bool {
        return $entry->type->id === 19;
    }];
            });

or
Event::on(
                Entry::class,
                Model::EVENT_DEFINE_RULES,
                function(DefineRulesEvent $event) {
                    $entry = $event->sender;
                    if ($entry->type->id === 19) {
                       $event->rules[] = [['expiryDate'], 'required'];
                    }
                });

> **Comments:**
> **username_0**: Ah very interesting, thank you! That's exactly what the issue was. So this was just the first step in getting a module to work, where I want to apply the date from another field to the expiry date - I'm thinking in that case `EVENT_BEFORE_SAVE` is probably what I need, but thank you for the additional info!  

---

