<p>For Craft 3, I <a href="https://craftcms.com/docs/3.x/extend/module-guide.html#preparation" rel="nofollow noreferrer">created a module</a> and added this:</p>
<pre><code>  \yii\base\Event::on(
        \craft\elements\Entry::class,
        \craft\base\Element::EVENT_BEFORE_SAVE,
        function (\craft\events\ModelEvent $e) {

            /* @var \craft\records\Entry $entry */
            $entry = $e-&gt;sender;

            //IF CAREERS ENTRY TYPE
            if ($entry-&gt;type == 'careers') {
                //IF &quot;Expiry date&quot; IS EMPTY
                if (!$entry-&gt;expiryDate) {

                    $entry-&gt;addError('expiryDate', 'Expiration date cannot be blank.');
                    $e-&gt;isValid = false;

                }
            }
        }
    );
</code></pre>
<p>Hope this helps!</p>

CC BY-SA 4.0
2021-05-14T02:07:03.643Z
2021-05-14T02:07:03.643Z
Paloma Martinez
<pre><code>    Event::on(
        Elements::class,
        Elements::EVENT_BEFORE_SAVE_ELEMENT,
        function (ElementEvent $event) {
            $element = $event-&gt;element;
            //IF CAREERS SECTION
            if ($element-&gt;section-&gt;id == 6) {
                //IF FIELD IS EMPTY
                if (!$element-&gt;expiryDate) {

  $event-&gt;params['entry']-&gt;addError('expiryDate', Craft::t('Expiration Date cannot be blank.'));
  $event-&gt;performAction = false;

                }
            }
        }
    );
</code></pre>

<p>Just not sure on this bit: </p>

<p>$event->params['entry']->addError('expiryDate', Craft::t('Expiration Date cannot be blank.'));</p>

<p>$event->performAction = false;</p>

CC BY-SA 4.0
2018-10-19T10:56:14.040Z
2018-10-19T10:56:14.040Z
Green17
<p>You could do this with a little custom plugin. All you need to do is to listen to <code>onBeforeSaveEntry</code> events
and check if there's an <code>expiryDate</code> submitted.</p>

<pre><code>craft()-&gt;on('entries.onBeforeSaveEntry', function(Event $event)
{
    $entry = $event-&gt;params['entry'];

    if($entry-&gt;section['handle'] == 'news' &amp;&amp; $entry-&gt;type['handle'] == 'news')
    {
        if ($entry-&gt;expiryDate === null)
        {
            $event-&gt;params['entry']-&gt;addError('expiryDate', Craft::t('Expiration Date cannot be blank.'));
            $event-&gt;performAction = false;
        }
    }
});
</code></pre>

CC BY-SA 3.0
2015-04-24T11:38:55.787Z
Perfect example, that worked great. Naturally, I was hoping for something native, that wouldn't be dependent on anything coded into the template, but with settings or things. I'll work on it :)
AJP
CC BY-SA 4.0
2018-10-17T09:26:30.900Z
Does anyone have a Craft 3 example of this? or a plugin?
Green17
CC BY-SA 3.0
2015-04-20T15:50:31.363Z
2015-04-20T16:04:29.557Z
2015-04-20T16:04:29.557Z
carlcs
<p>I can't find a way for a section (a channel in this case) to require the use to enter an expiration date.</p>

<p>I know I could add a new field and require that field, but that would get rid of the default functionality of entry/expire date fields.</p>

<expiration>
Is there a way to require the Expiration date
# Is there a way to require the Expiration date

(Asked by: username_0 on 2015-04-20)

I can't find a way for a section (a channel in this case) to require the use to enter an expiration date.

I know I could add a new field and require that field, but that would get rid of the default functionality of entry/expire date fields.

**Tags:** expiration


---

## 3 answers

### Answer by username_3 on 2015-04-20. Score: 4 | Accepted answer

You could do this with a little custom plugin. All you need to do is to listen to onBeforeSaveEntry events
and check if there's an expiryDate submitted.
craft()->on('entries.onBeforeSaveEntry', function(Event $event)
{
    $entry = $event->params['entry'];

    if($entry->section['handle'] == 'news' && $entry->type['handle'] == 'news')
    {
        if ($entry->expiryDate === null)
        {
            $event->params['entry']->addError('expiryDate', Craft::t('Expiration Date cannot be blank.'));
            $event->performAction = false;
        }
    }
});

> **Comments:**
> **username_0**: Perfect example, that worked great. Naturally, I was hoping for something native, that wouldn't be dependent on anything coded into the template, but with settings or things. I'll work on it :)  
> **username_2**: Does anyone have a Craft 3 example of this? or a plugin?  

---

### Answer by username_1 on 2021-05-14. Score: 1

For Craft 3, I created a module and added this:
  \yii\base\Event::on(
        \craft\elements\Entry::class,
        \craft\base\Element::EVENT_BEFORE_SAVE,
        function (\craft\events\ModelEvent $e) {

            /* @var \craft\records\Entry $entry */
            $entry = $e->sender;

            //IF CAREERS ENTRY TYPE
            if ($entry->type == 'careers') {
                //IF "Expiry date" IS EMPTY
                if (!$entry->expiryDate) {

                    $entry->addError('expiryDate', 'Expiration date cannot be blank.');
                    $e->isValid = false;

                }
            }
        }
    );

Hope this helps!


---

### Answer by username_2 on 2018-10-19. Score: 1

Event::on(
        Elements::class,
        Elements::EVENT_BEFORE_SAVE_ELEMENT,
        function (ElementEvent $event) {
            $element = $event->element;
            //IF CAREERS SECTION
            if ($element->section->id == 6) {
                //IF FIELD IS EMPTY
                if (!$element->expiryDate) {

  $event->params['entry']->addError('expiryDate', Craft::t('Expiration Date cannot be blank.'));
  $event->performAction = false;

                }
            }
        }
    );

Just not sure on this bit: 
$event->params['entry']->addError('expiryDate', Craft::t('Expiration Date cannot be blank.'));
$event->performAction = false;


---

