Skip to content

Commit

Permalink
Handle filters of type "date" (such as DOB) (CFM-295) (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
arlen authored Jun 26, 2023
1 parent c0a4a5f commit 37d9c58
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
3 changes: 3 additions & 0 deletions app/src/Lib/Traits/SearchFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public function whereFilter(\Cake\ORM\Query $query, string $attribute, string|ar
$sub = true;
} elseif(in_array($this->searchFilters[$attribute]['type'], $search_types, true)) {
return $query->where([$attribute => $search]);
} elseif($this->searchFilters[$attribute]['type'] == "date") {
// Parse the date string with FrozenTime to improve error handling
return $query->where([$attribute => FrozenTime::parseDate($search, 'y-M-d')]);
} elseif( $this->searchFilters[$attribute]['type'] == "timestamp") {
// Date between dates
if(!empty($search[0])
Expand Down
37 changes: 29 additions & 8 deletions app/templates/element/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,31 @@
$field_datetime_columns[$key] = $options;
continue;
}
$formParams = [
'label' => !empty($columns[$key]['label']) ? $columns[$key]['label'] : $options['label'],
// The default type is text, but we might convert to select below
'type' => 'text',
'value' => (!empty($query[$key]) ? $query[$key] : ''),
'required' => false,
];


if($options['type'] == 'date') {
// date picker
$formParams = [
'label' => !empty($columns[$key]['label']) ? $columns[$key]['label'] : $options['label'],
'type' => 'text', // date inputs must be text for accessibility reasons for now.
'value' => (!empty($query[$key]) ? $query[$key] : ''),
'required' => false,
'pattern' => '\d{4}-\d{2}-\d{2}',
'placeholder' => 'YYYY-MM-DD',
'title' => __d('field','datepicker.enterDate'),
'class' => 'form-control datepicker'
];
} else {
// text input
$formParams = [
'label' => !empty($columns[$key]['label']) ? $columns[$key]['label'] : $options['label'],
// The default type is text, but we might convert to select below
'type' => 'text',
'value' => (!empty($query[$key]) ? $query[$key] : ''),
'required' => false,
'class' => 'form-control'
];
}

// The populated variables are in plural while the column names are singular
// Convention: It is a prerequisite that the vvar should be the plural of the column name
$populated_vvar = Inflector::pluralize($key);
Expand Down Expand Up @@ -226,6 +243,8 @@
$coptions['required'] = false;
$coptions['placeholder'] = '';
// $coptions['placeholder'] = 'YYYY-MM-DD HH:MM:SS';
$coptions['pattern'] = '\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}';
$coptions['title'] = __d('field','datepicker.enterDateTime');
$coptions['id'] = str_replace("_", "-", $starts_field);

$pickerDate = '';
Expand Down Expand Up @@ -259,6 +278,8 @@
$coptions['required'] = false;
$coptions['placeholder'] = ''; // todo: Make this configurable
// $coptions['placeholder'] = 'YYYY-MM-DD HH:MM:SS';
$coptions['pattern'] = '\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}';
$coptions['title'] = __d('field','datepicker.enterDateTime');
$coptions['label'] = __d('field','ends_at');
$coptions['id'] = str_replace("_", "-", $ends_field);

Expand Down

0 comments on commit 37d9c58

Please sign in to comment.