Add validation for optional mandatory fields

Created on 3 November 2025, 4 days ago

Problem/Motivation

Some CRM entities require some fields that are not explicitly declared as required, and aren't currently picked up by the _civicrm_api3_validate() function.

Steps to reproduce

1. Go to /civicrm-contact/add
2. Set the Contact type to Household
3. Attempt to save the page.

The Household Name field is actually required, but isn't enforced as required leading to the page crashing:

The website encountered an unexpected error. Try again later.

Drupal\Core\Entity\EntityStorageException: Mandatory key(s) missing from params array: household_name in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 817 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).
Drupal\civicrm_entity\CiviCrmApi->save('contact', Array) (Line: 157)
Drupal\civicrm_entity\CiviEntityStorage->doSave(NULL, Object) (Line: 486)
Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 806)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object) (Line: 354)
Drupal\Core\Entity\EntityBase->save() (Line: 48)
Drupal\civicrm_entity\Entity\CivicrmEntity->save() (Line: 144)
Drupal\civicrm_entity\Form\CivicrmEntityForm->save(Array, Object)

The validation on the CiviCRM side is currently handled differently through custom form rules and the _civicrm_api3_contact_check_params() function.

Proposed resolution

During the entity validation step, dynamically check for the existence of the _civicrm_api3_{$entity}_check_params() (_civicrm_api3_contact_check_params() in this case) function, and use that to validate potentially required fields.

Initially thought to patch it in civicrm/civicrm-core so the validation also picks up optional fields, but since validation already works on there end, and this is a Drupal issue, it seemed easier to tackle it here.

It might still be worth tackling in civicrm/civicrm-core long term, as the validate function should ideally pick this up.

Remaining tasks

Provide MR.

User interface changes

N/A

🐛 Bug report
Status

Active

Version

4.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024