value pairs. * @method array getValues() Get field values. * * @package Civi\Api4\Generic */ abstract class AbstractCreateAction extends AbstractAction { /** * Field values to set for the new $ENTITY. * * @var array */ protected $values = []; /** * @param string $fieldName * @return mixed|null */ public function getValue(string $fieldName) { return $this->values[$fieldName] ?? NULL; } /** * Add a field value. * @param string $fieldName * @param mixed $value * @return $this */ public function addValue(string $fieldName, $value) { $this->values[$fieldName] = $value; return $this; } /** * @throws \API_Exception * @throws \Civi\API\Exception\UnauthorizedException */ protected function validateValues() { // FIXME: There should be a protocol to report a full list of errors... Perhaps a subclass of API_Exception? $unmatched = $this->checkRequiredFields($this->getValues()); if ($unmatched) { throw new \API_Exception("Mandatory values missing from Api4 {$this->getEntityName()}::{$this->getActionName()}: " . implode(", ", $unmatched), "mandatory_missing", ["fields" => $unmatched]); } if ($this->checkPermissions && !CoreUtil::checkAccessRecord($this, $this->getValues(), \CRM_Core_Session::getLoggedInContactID() ?: 0)) { throw new UnauthorizedException("ACL check failed"); } $e = new ValidateValuesEvent($this, [$this->getValues()], new \CRM_Utils_LazyArray(function () { return [['old' => NULL, 'new' => $this->getValues()]]; })); \Civi::dispatcher()->dispatch('civi.api4.validate', $e); if (!empty($e->errors)) { throw $e->toException(); } } }