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