<p>You would need to set the entry's scenario to <code>live</code> just before saving the element.</p>
<pre class="lang-php prettyprint-override"><code>$section = Craft::$app-&gt;sections-&gt;getSectionByHandle('contactSubmissions');

$entry = new Entry();
$entry-&gt;sectionId = $section-&gt;id;
$entry-&gt;typeId = 2;
$entry-&gt;authorId = 1;
$entry-&gt;enabled = true;

$entry-&gt;setFieldValues([
    'contactName' =&gt; ucwords($data['contactName']),
    'contactEmail' =&gt; $data['contactEmail'],
    'contactCompany' =&gt; $data['contactCompany'],
    'contactMessage' =&gt; $data['contactMessage'],
    'contactHeardAbout' =&gt; $data['contactHeardAbout'],
    'contactWebsite' =&gt; $data['contactWebsite'],
    'contactTitle' =&gt; $data['contactTitle'],
]);

$entry-&gt;setScenario(Entry::SCENARIO_LIVE);

if ( Craft::$app-&gt;elements-&gt;saveElement($entry) ) {
    //$this-&gt;sendMail($entry);
}

die(var_dump($entry-&gt;getErrors()));

return $entry;
</code></pre>
<p>Alternatively, If you want to save entries from the front-end, unless you have very specific requirements, you could use:</p>
<ul>
<li><a href="https://plugins.craftcms.com/guest-entries" rel="nofollow noreferrer">Guest entries</a> plugin (if users are not logged in)</li>
<li>Make a <a href="https://craftcms.com/knowledge-base/entry-form" rel="nofollow noreferrer">front-end entry form</a> (if users are logged in)</li>
</ul>

CC BY-SA 4.0
2021-03-31T13:21:22.757Z
Thank you and Brandon for this! I wouldn't have been able to figure out this one
Cat Named Dog
CC BY-SA 4.0
2021-03-30T06:21:53.520Z
2021-03-30T06:21:53.520Z
Oli
<p>I'm trying to save an entry via a controller. They are contact form submission. A few of the fields are required.</p>
<pre><code>    $section = Craft::$app-&gt;sections-&gt;getSectionByHandle('contactSubmissions');

    $entry = new Entry();
    $entry-&gt;sectionId = $section-&gt;id;
    $entry-&gt;typeId = 2;
    $entry-&gt;authorId = 1;
    $entry-&gt;enabled = true;

    $entry-&gt;setFieldValues([
        'contactName' =&gt; ucwords($data['contactName']),
        'contactEmail' =&gt; $data['contactEmail'],
        'contactCompany' =&gt; $data['contactCompany'],
        'contactMessage' =&gt; $data['contactMessage'],
        'contactHeardAbout' =&gt; $data['contactHeardAbout'],
        'contactWebsite' =&gt; $data['contactWebsite'],
        'contactTitle' =&gt; $data['contactTitle'],
    ]);

    
    if ( Craft::$app-&gt;elements-&gt;saveElement($entry) ) {
        //$this-&gt;sendMail($entry);
    }
    
    die(var_dump($entry-&gt;getErrors()));

    return $entry;
</code></pre>
<p>The only validation that runs if the missing title, which in this case is the contactName. All other keys in the $data array are empty strings. They do not trigger any validation and the empty entry saves into the database.</p>
<p>If I were to go into the control panel and try to save the entry, it would fail with the validation errors.</p>
<p>If I were to enter a value for the email field, but have an invalid email, that validation then gets triggered. But none of the empty fields do.</p>
<hr />
<p>Additional info</p>
<p>Craft version: 3.6.10</p>
<p>PHP version: 7.3.13</p>

<entry>
Validation not running on empty custom fields on an entry (when creating programmatically)
# Validation not running on empty custom fields on an entry (when creating programmatically)

(Asked by: username_0 on 2021-03-29)

I'm trying to save an entry via a controller. They are contact form submission. A few of the fields are required.
    $section = Craft::$app->sections->getSectionByHandle('contactSubmissions');

    $entry = new Entry();
    $entry->sectionId = $section->id;
    $entry->typeId = 2;
    $entry->authorId = 1;
    $entry->enabled = true;

    $entry->setFieldValues([
        'contactName' => ucwords($data['contactName']),
        'contactEmail' => $data['contactEmail'],
        'contactCompany' => $data['contactCompany'],
        'contactMessage' => $data['contactMessage'],
        'contactHeardAbout' => $data['contactHeardAbout'],
        'contactWebsite' => $data['contactWebsite'],
        'contactTitle' => $data['contactTitle'],
    ]);

    
    if ( Craft::$app->elements->saveElement($entry) ) {
        //$this->sendMail($entry);
    }
    
    die(var_dump($entry->getErrors()));

    return $entry;

The only validation that runs if the missing title, which in this case is the contactName. All other keys in the $data array are empty strings. They do not trigger any validation and the empty entry saves into the database.
If I were to go into the control panel and try to save the entry, it would fail with the validation errors.
If I were to enter a value for the email field, but have an invalid email, that validation then gets triggered. But none of the empty fields do.

Additional info
Craft version: 3.6.10
PHP version: 7.3.13

**Tags:** entry


---

## 1 answers

### Answer by username_1 on 2021-03-30. Score: 2 | Accepted answer

You would need to set the entry's scenario to live just before saving the element.
$section = Craft::$app->sections->getSectionByHandle('contactSubmissions');

$entry = new Entry();
$entry->sectionId = $section->id;
$entry->typeId = 2;
$entry->authorId = 1;
$entry->enabled = true;

$entry->setFieldValues([
    'contactName' => ucwords($data['contactName']),
    'contactEmail' => $data['contactEmail'],
    'contactCompany' => $data['contactCompany'],
    'contactMessage' => $data['contactMessage'],
    'contactHeardAbout' => $data['contactHeardAbout'],
    'contactWebsite' => $data['contactWebsite'],
    'contactTitle' => $data['contactTitle'],
]);

$entry->setScenario(Entry::SCENARIO_LIVE);

if ( Craft::$app->elements->saveElement($entry) ) {
    //$this->sendMail($entry);
}

die(var_dump($entry->getErrors()));

return $entry;

Alternatively, If you want to save entries from the front-end, unless you have very specific requirements, you could use:

Guest entries plugin (if users are not logged in)
Make a front-end entry form (if users are logged in)

> **Comments:**
> **username_0**: Thank you and Brandon for this! I wouldn't have been able to figure out this one  

---

