Skip to content

Commit

Permalink
FIX Pass filter fields through the form before using them.
Browse files Browse the repository at this point in the history
Some form fields such as CurrencyField manipulate the value to give more
pure values, e.g. removing a currency symbol before the actual value.
  • Loading branch information
GuySartorelli committed Feb 17, 2025
1 parent 0c31c3f commit 0471eac
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Forms/CurrencyField.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function setValue($value, $data = null)
. number_format((double)preg_replace('/[^0-9.\-]/', '', $value ?? ''), 2);
return $this;
}

/**
* Overwrite the datavalue before saving to the db ;-)
* return 0.00 if no value, or value is non-numeric
Expand Down
20 changes: 17 additions & 3 deletions src/Forms/GridField/GridFieldFilterHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,13 @@ public function handleAction(GridField $gridField, $actionName, $arguments, $dat
$state->Columns = [];

if ($actionName === 'filter') {
if (isset($data['filter'][$gridField->getName()])) {
foreach ($data['filter'][$gridField->getName()] as $key => $filter) {
$state->Columns->$key = $filter;
$filterValues = $data['filter'][$gridField->getName()] ?? null;
if ($filterValues !== null) {
$form = $this->getSearchForm($gridField);
$this->removeSearchPrefixFromFields($form->Fields());
$form->loadDataFrom($filterValues);
foreach ($filterValues as $fieldName => $rawFilterValue) {
$state->Columns->$fieldName = $form->Fields()->dataFieldByName($fieldName)?->dataValue() ?? $rawFilterValue;
}
}
}
Expand Down Expand Up @@ -498,6 +502,16 @@ private function addSearchPrefixToFields(FieldList $fields): void
}
}

private function removeSearchPrefixFromFields(FieldList $fields): void
{
foreach ($fields as $field) {
$field->setName(str_replace('Search__', '', $field->getName()));
if ($field instanceof CompositeField) {
$this->removeSearchPrefixFromFields($field->getChildren());
}
}
}

/**
* Update CSS classes for form fields, including nested inside composite fields
*/
Expand Down

0 comments on commit 0471eac

Please sign in to comment.