diff --git a/app/plugins/CoreEnroller/resources/locales/en_US/core_enroller.po b/app/plugins/CoreEnroller/resources/locales/en_US/core_enroller.po
index 2462db1cb..a60173e81 100644
--- a/app/plugins/CoreEnroller/resources/locales/en_US/core_enroller.po
+++ b/app/plugins/CoreEnroller/resources/locales/en_US/core_enroller.po
@@ -109,6 +109,9 @@ msgstr "DefaultVerificationValidity"
msgid "error.ApprovalCollectors.comment"
msgstr "Comment is required"
+msgid "error.attr.invalid"
+msgstr "Please correct the following: {0}"
+
msgid "error.CouSelectors.cou_id.invalid"
msgstr "Requested COU {0} is not a valid option"
diff --git a/app/plugins/CoreEnroller/src/Controller/AttributeCollectorsController.php b/app/plugins/CoreEnroller/src/Controller/AttributeCollectorsController.php
index e06d3eb05..ab1bf70bd 100644
--- a/app/plugins/CoreEnroller/src/Controller/AttributeCollectorsController.php
+++ b/app/plugins/CoreEnroller/src/Controller/AttributeCollectorsController.php
@@ -30,6 +30,7 @@
namespace CoreEnroller\Controller;
use App\Controller\StandardEnrollerController;
+use App\Lib\Exception\ValidationException;
use Cake\Event\EventInterface;
use Cake\Http\Response;
use Cake\ORM\TableRegistry;
@@ -76,6 +77,15 @@ public function dispatch(string $id) {
comment: __d('core_enroller', 'result.attr.saved')
);
}
+ catch(ValidationException $e) {
+ // List each invalid field, so the Enrollee can correct the values and resubmit
+ $items = array_map(fn($m) => '
' . h($m) . '', $e->getMessages());
+
+ $this->Flash->error(
+ __d('core_enroller', 'error.attr.invalid', ['' . implode('', $items) . '
']),
+ ['escape' => false]
+ );
+ }
catch(\Exception $e) {
$this->Flash->error($e->getMessage());
}
diff --git a/app/plugins/CoreEnroller/src/Model/Table/AttributeCollectorsTable.php b/app/plugins/CoreEnroller/src/Model/Table/AttributeCollectorsTable.php
index f29a7f081..cb6390ae0 100644
--- a/app/plugins/CoreEnroller/src/Model/Table/AttributeCollectorsTable.php
+++ b/app/plugins/CoreEnroller/src/Model/Table/AttributeCollectorsTable.php
@@ -31,6 +31,7 @@
use App\Lib\Enum\PetitionActionEnum;
use App\Lib\Enum\StatusEnum;
+use App\Lib\Exception\ValidationException;
use App\Model\Entity\Person;
use App\Model\Entity\PersonRole;
use Cake\Collection\Collection;
@@ -42,6 +43,7 @@
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Utility\Hash;
+use Cake\Utility\Inflector;
use Cake\Validation\Validator;
class AttributeCollectorsTable extends Table {
@@ -611,6 +613,45 @@ protected function enforceCouAttributes(int $id, int $petitionId, array $attribu
return $attributes;
}
+ /**
+ * Parse the submitted dispatch form fields into Enrollment Attribute values.
+ * Form keys are either field- (single valued attributes) or
+ * field-- (multi valued attributes, eg Names or Addresses).
+ *
+ * @since COmanage Registry v5.4.0
+ * @param array $attributes Submitted form fields
+ * @return array Values, keyed on Enrollment Attribute ID and then column name ('' for single valued attributes)
+ */
+
+ protected function parseFormAttributes(array $attributes): array {
+ $parsed = [];
+
+ foreach($attributes as $enrollmentAttributeLabel => $value) {
+ // Remove field- prefix from the form field name
+ $fieldToParts = explode('-', (string)$enrollmentAttributeLabel);
+ // MVAs have multiple columns. For example a name has:
+ // given, surname, prefix, ...
+ $columnName = '';
+ if (count($fieldToParts) > 2) {
+ // There is a type ID in the middle
+ $columnName = $fieldToParts[1];
+ }
+ // The enrollment Attribute ID is the last part
+ $enrollmentAttributeId = (int)array_pop($fieldToParts);
+
+ // The people picker will send a complex string and not just the id. We need to extract it ourselves.
+ $re = '/^.*\(ID: (\d+)\)$/m';
+ preg_match_all($re, (string)$value, $matches, PREG_SET_ORDER, 0);
+ if(!empty($matches)) {
+ $value = $matches[0][1];
+ }
+
+ $parsed[$enrollmentAttributeId][$columnName] = $value;
+ }
+
+ return $parsed;
+ }
+
/**
* Insert or update a set of Petition Attributes.
*
@@ -628,66 +669,61 @@ public function upsert(int $id, int $petitionId, array $attributes) {
// Enforce the COU server side, before anything is stored
$attributes = $this->enforceCouAttributes($id, $petitionId, $attributes);
+ // Validate the submitted values against the target models, before anything is stored
+ $this->validateAttributes($id, $petitionId, $attributes);
+
// Do we have existing attributes for this petition? Note this will pull
// _all_ attributes for the Petition, not just those associated with this
// particular Attribute Collector; however we'll only look at the attributes
- // we need below.
- $currentAttributes = $this->EnrollmentAttributes
- ->PetitionAttributes->find('list',
- keyField: 'enrollment_attribute_id',
- valueField: 'id',
- )
- ->where(['petition_id' => $petitionId])
- ->toArray();
+ // we need below. Multi valued attributes (eg Names or Telephone Numbers) have
+ // one Petition Attribute per column, so these are keyed on Enrollment Attribute
+ // ID and then column name ('' for single valued attributes).
+ $currentAttributes = [];
+
+ $existing = $this->EnrollmentAttributes
+ ->PetitionAttributes->find()
+ ->select(['id', 'enrollment_attribute_id', 'column_name'])
+ ->where(['petition_id' => $petitionId])
+ ->all();
+
+ foreach($existing as $pa) {
+ $currentAttributes[(int)$pa->enrollment_attribute_id][(string)($pa->column_name ?? '')] = (int)$pa->id;
+ }
$petitionAttributes = [];
- foreach($attributes as $enrollmentAttributeLabel => $value) {
- // Remove field- prefix from the form field name
- $fieldToParts = explode('-', $enrollmentAttributeLabel);
- // MVAs have multiple columns. For example a name has:
- // given, surname, prefix, ...
- $columnName = null;
- if (count($fieldToParts) > 2) {
- // There is a type ID in the middle
- $columnName = $fieldToParts[1];
- }
- // The enrollment Attribute ID is the last part
- $enrollmentAttributeId = (int)array_pop($fieldToParts);
+ foreach($this->parseFormAttributes($attributes) as $enrollmentAttributeId => $columns) {
+ foreach($columns as $column => $value) {
+ // An empty column key means a single valued attribute
+ $columnName = ($column === '') ? null : (string)$column;
- // The people picker will send a complex string and not just the id. We need to extract it ourselves.
- $re = '/^.*\(ID: (\d+)\)$/m';
- preg_match_all($re, $value, $matches, PREG_SET_ORDER, 0);
- if(!empty($matches)) {
- $value = $matches[0][1];
- }
+ $newAttribute = [
+ 'petition_id' => $petitionId,
+ 'enrollment_attribute_id' => $enrollmentAttributeId,
+ 'value' => $value,
+ // This is the column name for the attributes that consist of multiple fields, like the name or the address
+ 'column_name' => $columnName,
+ ];
- $newAttribute = [
- 'petition_id' => $petitionId,
- 'enrollment_attribute_id' => $enrollmentAttributeId,
- 'value' => $value,
- // This is the column name for the attributes that consist of multiple fields, like the name or the address
- 'column_name' => $columnName,
- ];
+ if(isset($currentAttributes[$enrollmentAttributeId][(string)$column])) {
+ // This is an update of an existing attribute
- if(\array_key_exists($enrollmentAttributeId, $currentAttributes)) {
- // This is an update of an existing attribute
+ $newAttribute['id'] = $currentAttributes[$enrollmentAttributeId][(string)$column];
- $newAttribute['id'] = $currentAttributes[$enrollmentAttributeId];
+ $entity = $this->EnrollmentAttributes
+ ->PetitionAttributes->get($newAttribute['id']);
- $entity = $this->EnrollmentAttributes
- ->PetitionAttributes->get($newAttribute['id']);
+ // We don't bother with patch entity since the only thing we support
+ // changing is value
- // We don't bother with patch entity since the only thing we support
- // changing is value
+ $entity->value = $value;
- $entity->value = $value;
-
- $this->EnrollmentAttributes->PetitionAttributes->saveOrFail($entity);
+ $this->EnrollmentAttributes->PetitionAttributes->saveOrFail($entity);
// XXX we could record petition history that this specific attribute was updated
- } else {
- // This is a new attribute
- $petitionAttributes[] = $newAttribute;
+ } else {
+ // This is a new attribute
+ $petitionAttributes[] = $newAttribute;
+ }
}
}
@@ -713,6 +749,212 @@ public function upsert(int $id, int $petitionId, array $attributes) {
return true;
}
+ /**
+ * Validate the submitted dispatch form fields against the validation rules of the
+ * models they will be saved to at finalization (eg Telephone Numbers or Person Roles),
+ * so that invalid values can be corrected before the step completes.
+ *
+ * @since COmanage Registry v5.4.0
+ * @param int $id Attribute Collector ID
+ * @param int $petitionId Petition ID
+ * @param array $attributes Submitted form fields
+ * @throws ValidationException
+ */
+
+ public function validateAttributes(int $id, int $petitionId, array $attributes): void {
+ $parsed = $this->parseFormAttributes($attributes);
+
+ if(empty($parsed)) {
+ return;
+ }
+
+ $enrollmentAttributes = $this->EnrollmentAttributes
+ ->find()
+ ->where([
+ 'attribute_collector_id' => $id,
+ 'status' => StatusEnum::Active
+ ])
+ ->where(fn(QueryExpression $exp, Query $q) => $exp->in('id', array_keys($parsed)))
+ ->orderBy(['ordr' => 'ASC'])
+ ->toArray();
+
+ if(empty($enrollmentAttributes)) {
+ return;
+ }
+
+ $supportedAttributes = $this->EnrollmentAttributes->supportedAttributes();
+ $coId = (int)TableRegistry::getTableLocator()->get('Petitions')->findCoForRecord($petitionId);
+ $now = FrozenTime::now();
+
+ // Single valued MVEAs are stored in this column of the MVEA model
+ $valueColumns = [
+ 'adHocAttribute' => 'value',
+ 'emailAddress' => 'mail',
+ 'identifier' => 'identifier',
+ 'pronoun' => 'pronouns',
+ 'url' => 'url'
+ ];
+
+ // Single valued Person and Person Role attributes are combined into one record each,
+ // the same way hydrate() saves them
+ $combined = [
+ 'Person' => ['table' => 'People', 'data' => [], 'labels' => []],
+ 'PersonRole' => ['table' => 'PersonRoles', 'data' => [], 'labels' => []]
+ ];
+
+ $errors = [];
+
+ foreach($enrollmentAttributes as $ea) {
+ $attr = $supportedAttributes[$ea->attribute] ?? null;
+
+ if(empty($attr)) {
+ continue;
+ }
+
+ $label = !empty($ea->label) ? $ea->label : $attr['label'];
+ $values = $parsed[(int)$ea->id];
+
+ if(!empty($attr['mveaModel'])) {
+ $data = [];
+
+ foreach($values as $column => $value) {
+ if($column === '') {
+ if(!isset($valueColumns[$ea->attribute])) {
+ continue;
+ }
+ $column = $valueColumns[$ea->attribute];
+ }
+ $data[(string)$column] = $value;
+ }
+
+ $isEmpty = empty(array_filter($data, fn($v) => $v !== null && trim((string)$v) !== ''));
+
+ // Optional MVEAs left empty are skipped, as they are not saved at finalization
+ if(empty($data) || ($isEmpty && !$ea->required)) {
+ continue;
+ }
+
+ if($ea->attribute === 'adHocAttribute') {
+ if(!$ea->required && ($ea->attribute_tag === null || trim((string)$ea->attribute_tag) === '')) {
+ continue;
+ }
+ $data['tag'] = $ea->attribute_tag;
+ } else {
+ $data['type_id'] = $ea->attribute_type;
+ }
+
+ if(in_array($ea->attribute, ['name', 'pronoun'], true) && !empty($ea->attribute_language)) {
+ $data['language'] = $ea->attribute_language;
+ }
+
+ $this->collectValidationErrors($attr['mveaModel'], $coId, $data, $label, $errors);
+ } elseif(($attr['model'] ?? null) === 'Group') {
+ // Each Group Membership is saved as its own record
+ $value = $values[''] ?? reset($values);
+
+ if($value === null || $value === '') {
+ continue;
+ }
+
+ $this->collectValidationErrors('GroupMembers', $coId, [$ea->attribute => $value], $label, $errors);
+ } elseif(isset($combined[$attr['model'] ?? ''])) {
+ $value = $values[''] ?? reset($values);
+
+ if($ea->attribute === 'valid_from' || $ea->attribute === 'valid_through') {
+ try {
+ $value = $this->EnrollmentAttributes->resolveValidityDate($ea->attribute, $value, $ea, $now);
+ } catch(\Exception $e) {
+ $errors[$this->validationErrorLabel($label, $ea->attribute)] = [
+ 'content' => __d('error', 'invalid', [(string)$value])
+ ];
+ continue;
+ }
+ }
+
+ $combined[$attr['model']]['data'][$ea->attribute] = $value;
+ $combined[$attr['model']]['labels'][$ea->attribute] = $label;
+ }
+ // Petition attributes are only stored in the Petition, so there is nothing to validate
+ }
+
+ foreach($combined as $c) {
+ if(!empty($c['data'])) {
+ $this->collectValidationErrors($c['table'], $coId, $c['data'], $c['labels'], $errors);
+ }
+ }
+
+ if(!empty($errors)) {
+ throw new ValidationException($errors);
+ }
+ }
+
+ /**
+ * Validate a record against the default validator of a target model, and collect
+ * the errors on the validated fields.
+ *
+ * @since COmanage Registry v5.4.0
+ * @param string $tableName Target table name (eg TelephoneNumbers)
+ * @param int $coId CO ID, used for CO Settings dependent validation rules
+ * @param array $data Record to validate
+ * @param string|array $label Attribute label, or array of field => Attribute label
+ * @param array $errors Collected errors, keyed on " ()"
+ */
+
+ protected function collectValidationErrors(
+ string $tableName,
+ int $coId,
+ array $data,
+ string|array $label,
+ array &$errors
+ ): void {
+ $table = TableRegistry::getTableLocator()->get($tableName);
+
+ // Some validators (eg Telephone Numbers) depend on CO Settings, so the CO must
+ // be known before the validator is built
+ if(!empty($coId) && method_exists($table, 'setCurCoId')) {
+ $table->setCurCoId($coId);
+ }
+
+ // Foreign keys to the Person, Person Role or Group are only known at finalization
+ $ignore = ['person_id', 'person_role_id', 'group_id'];
+ if(method_exists($table, 'getPrimaryLinks')) {
+ $ignore = array_merge($ignore, $table->getPrimaryLinks());
+ }
+ // ... unless the key is the collected value itself (eg the Group of a Group Membership)
+ $ignore = array_diff($ignore, array_keys($data));
+
+ $result = $table->getValidator()->validate($data, true);
+
+ foreach($result as $field => $fieldErrors) {
+ if(in_array($field, $ignore, true) || !array_key_exists($field, $data)) {
+ continue;
+ }
+
+ $attrLabel = is_array($label) ? ($label[$field] ?? $field) : $label;
+
+ $errors[$this->validationErrorLabel($attrLabel, (string)$field)] = $fieldErrors;
+ }
+ }
+
+ /**
+ * Construct the label used to report a validation error.
+ *
+ * @since COmanage Registry v5.4.0
+ * @param string $attrLabel Enrollment Attribute label
+ * @param string $field Field name
+ * @return string Label, as " ()"
+ */
+
+ protected function validationErrorLabel(string $attrLabel, string $field): string {
+ $fieldLabel = __d('field', $field);
+
+ if($fieldLabel === $field) {
+ $fieldLabel = Inflector::humanize($field);
+ }
+
+ return $attrLabel . ' (' . $fieldLabel . ')';
+ }
+
/**
* Set validation rules.
*
diff --git a/app/plugins/CoreEnroller/templates/element/fieldDiv.php b/app/plugins/CoreEnroller/templates/element/fieldDiv.php
index 0f1c84764..bc81dcdf0 100644
--- a/app/plugins/CoreEnroller/templates/element/fieldDiv.php
+++ b/app/plugins/CoreEnroller/templates/element/fieldDiv.php
@@ -35,7 +35,7 @@
print $this->element('form/nameDiv');
// This configuration isn't necessary anymore.
- if(isset($vv_field_arguments['fieldDescription'])) {
+ if(array_key_exists('fieldDescription', $vv_field_arguments)) {
unset($vv_field_arguments['fieldDescription']);
$this->set('vv_field_arguments', $vv_field_arguments);
}
diff --git a/app/src/Lib/Exception/ValidationException.php b/app/src/Lib/Exception/ValidationException.php
new file mode 100644
index 000000000..d33833e6c
--- /dev/null
+++ b/app/src/Lib/Exception/ValidationException.php
@@ -0,0 +1,101 @@
+ message
+ protected array $errors = [];
+
+ /**
+ * Constructor.
+ *
+ * @since COmanage Registry v5.4.0
+ * @param array $errors Validation errors, keyed on field (or label), each an array of rule => message
+ */
+
+ public function __construct(array $errors) {
+ $this->errors = $errors;
+
+ parent::__construct(implode('; ', $this->getMessages()));
+ }
+
+ /**
+ * Construct a Validation Exception from the errors of an entity, eg after a failed save.
+ *
+ * @since COmanage Registry v5.4.0
+ * @param EntityInterface $entity Entity with errors
+ * @return static Validation Exception
+ */
+
+ public static function fromEntity(EntityInterface $entity): static {
+ return new static($entity->getErrors());
+ }
+
+ /**
+ * Obtain the validation errors.
+ *
+ * @since COmanage Registry v5.4.0
+ * @return array Validation errors, keyed on field (or label), each an array of rule => message
+ */
+
+ public function getErrors(): array {
+ return $this->errors;
+ }
+
+ /**
+ * Obtain the validation errors as a list of human readable messages.
+ *
+ * @since COmanage Registry v5.4.0
+ * @return array Messages, each formatted as ": "
+ */
+
+ public function getMessages(): array {
+ $messages = [];
+
+ foreach($this->errors as $label => $fieldErrors) {
+ foreach((array)$fieldErrors as $msg) {
+ // Nested entity errors (eg associated records) are flattened
+ if(is_array($msg)) {
+ $msg = implode(', ', array_filter(array_map(
+ fn($m) => is_scalar($m) ? (string)$m : null,
+ Hash::flatten($msg)
+ )));
+ }
+
+ $messages[] = $label . ': ' . $msg;
+ }
+ }
+
+ return $messages;
+ }
+}
diff --git a/app/templates/element/form/fieldDiv.php b/app/templates/element/form/fieldDiv.php
index 53ca80019..3ce9187fd 100644
--- a/app/templates/element/form/fieldDiv.php
+++ b/app/templates/element/form/fieldDiv.php
@@ -35,7 +35,7 @@
print $this->element('form/nameDiv');
// This configuration isn't necessary anymore.
- if(isset($vv_field_arguments['fieldDescription'])) {
+ if(array_key_exists('fieldDescription', $vv_field_arguments)) {
unset($vv_field_arguments['fieldDescription']);
$this->set('vv_field_arguments', $vv_field_arguments);
}