Skip to content

Commit

Permalink
Add FieldHelper support for textarea. Refactored fieldset-group.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis committed Nov 13, 2024
1 parent c59e684 commit e7aae1f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 130 deletions.
107 changes: 0 additions & 107 deletions app/plugins/CoreEnroller/templates/AttributeCollectors/attribute.inc

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@

if(isset($supportedAttributes['mveaModel'])) {
$supportedAttributes = $this->Petition->getSupportedEnrollmentAttribute($attr->attribute);
$tableValidator = $this->Petition->getTableValidator($supportedAttributes['mveaModel']);
$isRequiredFromValidationRule = !$tableValidator->field($field)->isEmptyAllowed();
$modelTable = $this->Petition->getTable($supportedAttributes['mveaModel']);
$isRequiredFromValidationRule = !$modelTable->getValidator()->field($field)->isEmptyAllowed();
}

// Construct the field arguments
Expand All @@ -50,7 +50,7 @@
// problems with field names that are purely integers (even if cast to strings)
'fieldName' => "field-$field-$attr->id",
'fieldLabel' => $attr->label, // fieldLabel is only applicable to checkboxes
'fieldType' => $supportedAttributes['fieldType'],
'fieldType' => $modelTable->getSchema()->getColumn($field)['type'],
'fieldNameAlias' => $attr->attribute // the field name to its enrollment attribute field name
];
?>
Expand Down
28 changes: 12 additions & 16 deletions app/plugins/CoreEnroller/templates/element/mveas/fieldset-group.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,28 @@
if (!empty($cosettings[0][$permitted_fields_variable_name])) {
$permitted_fields_list = explode(',', $cosettings[0][$permitted_fields_variable_name]);
}
// Address has no permitted fields configuration at CO level. And according to our design,
// the permitted fields are found in CoSettings.
// For all the attributes that have no permitted configuration at the CO level,
// we will get all the fields that we support
// We will construct the list of permitted fields from the list of required fields.
// The permitted fields will be the set that contains all the required fields.
$requiredDropDownListVar = $attr->attribute . 'RequiredFields';
if(empty($permitted_fields_list) && !empty($$requiredDropDownListVar)) {
usort($$requiredDropDownListVar, static fn($a, $b) => count(explode(',', $a)) <=> count(explode(',', $b)));
$permitted_fields = end($$requiredDropDownListVar);
$permitted_fields_list = collection(explode(',', $permitted_fields))->map(fn($value) => trim($value));
// Address has no permitted fields configuration at CO level. We will get them from
// the model configuration
$supportedAttributes = $this->Petition->getSupportedEnrollmentAttribute($attr->attribute);
$modelTable = $this->Petition->getTable($supportedAttributes['mveaModel']);
if(empty($permitted_fields_list) && !empty($modelTable?->getPermittedFields())) {
$permitted_fields_list = $modelTable->getPermittedFields();
}

$permitted_fields_list_flipped = array_flip($permitted_fields_list);

?>

<?php foreach($groupedFieldsArray as $idx => $fields): ?>
<div class="fieldset-subgroup">
<?php
$permitted_fields_list = $permitted_fields_list->map(static fn($value) => str_replace(' ', '_', Inflector::underscore($value)));
foreach($fields as $field) {
if($permitted_fields_list->contains($field)) {
if(isset($permitted_fields_list_flipped[$field])) {
// Print the element
print $this->element('CoreEnroller.mveas/fieldset-field', compact('field', 'attr'));
}
// Remove the field we render from the permitted list.
$permitted_fields_list = $permitted_fields_list->filter(fn($value) => $value !== $field);
// Remove the field we rendered from the permitted list.
unset($permitted_fields_list_flipped[$field]);
}
?>
</div>
Expand All @@ -78,7 +74,7 @@
<?php

// For all the remaining fields we do not need a group
foreach ($permitted_fields_list as $field) {
foreach (array_flip($permitted_fields_list_flipped) as $field) {
print $this->element('CoreEnroller.mveas/fieldset-field', compact('field', 'attr'));
}
?>
Expand Down
13 changes: 13 additions & 0 deletions app/src/Model/Table/AddressesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class AddressesTable extends Table {
'postal'
]
];

// Default permitted Fields. Used for the Attribute Collection
private $permittedFields = ['locality', 'state', 'postal_code', 'country', 'street', 'room'];

/**
* Perform Cake Model initialization.
Expand Down Expand Up @@ -262,4 +265,14 @@ public function validationDefault(Validator $validator): Validator {

return $validator;
}

/**
* Get the hardcoded list of the Default Permitted Fields
*
* @since COmanage Registry v5.0.0
* @return array List of permitted fields
*/
public function getPermittedFields(): array {
return $this->permittedFields;
}
}
1 change: 1 addition & 0 deletions app/src/View/Helper/FieldHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ public function formField(string $fieldName,
'class' => 'form-check-input',
]),
'select' => $this->Form->select($fieldName, $fieldSelectOptions, $fieldArgs),
'text' => $this->Form->textarea($fieldName, $fieldArgs),
'date' => $this->dateField(fieldName: $fieldName, dateType: DateTypeEnum::DateOnly, fieldArgs: $fieldArgs),
'datetime',
'timestamp' => $this->dateField(fieldName: $fieldName, fieldArgs: $fieldArgs),
Expand Down
7 changes: 3 additions & 4 deletions app/src/View/Helper/PetitionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,11 @@ public function populateAutoViewVars(): void
*
* @param string $tableName
*
* @return Validator
* @return Table
* @since COmanage Registry v5.0.0
*/
public function getTableValidator(string $tableName): \Cake\Validation\Validator
public function getTable(string $tableName): Table
{
$ModelTable = TableRegistry::getTableLocator()->get($tableName);
return $ModelTable->getValidator();// For MVEAs we retrieve the validation rules from CoSettings. Telephone numbers are different
return TableRegistry::getTableLocator()->get($tableName);
}
}

0 comments on commit e7aae1f

Please sign in to comment.