Merge pull request #8414 from creative-commoners/pulls/4/searching-for-a-name-conflict

FIX Avoid having search fields with the same names as form elements
This commit is contained in:
Robbie Averill 2018-10-02 13:43:59 +02:00 committed by GitHub
commit c8e7672ede
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 3 deletions

View File

@ -235,11 +235,19 @@ class GridFieldFilterHeader implements GridField_URLHandler, GridField_HTMLProvi
$name = $gridField->Title ?: singleton($gridField->getModelClass())->i18n_plural_name();
// Prefix "Search__" onto the filters for the React component
$filters = $context->getSearchParams();
if (!$this->useLegacyFilterHeader && !empty($filters)) {
$filters = array_combine(array_map(function ($key) {
return 'Search__' . $key;
}, array_keys($filters)), $filters);
}
$schema = [
'formSchemaUrl' => $schemaUrl,
'name' => $searchField,
'placeholder' => _t(__CLASS__ . '.Search', 'Search "{name}"', ['name' => $name]),
'filters' => $context->getSearchParams() ?: new \stdClass, // stdClass maps to empty json object '{}'
'filters' => $filters ?: new \stdClass, // stdClass maps to empty json object '{}'
'gridfield' => $gridField->getName(),
'searchAction' => GridField_FormAction::create($gridField, 'filter', false, 'filter', null)->getAttribute('name'),
'clearAction' => GridField_FormAction::create($gridField, 'reset', false, 'reset', null)->getAttribute('name')
@ -264,6 +272,11 @@ class GridFieldFilterHeader implements GridField_URLHandler, GridField_HTMLProvi
return new HTTPResponse(_t(__CLASS__ . '.SearchFormFaliure', 'No search form could be generated'), 400);
}
// Append a prefix to search field names to prevent conflicts with other fields in the search form
foreach ($searchFields as $field) {
$field->setName('Search__' . $field->getName());
}
$columns = $gridField->getColumns();
// Update field titles to match column titles
@ -289,6 +302,7 @@ class GridFieldFilterHeader implements GridField_URLHandler, GridField_HTMLProvi
$searchFields,
new FieldList()
);
$form->setFormMethod('get');
$form->setFormAction($gridField->Link());
$form->addExtraClass('cms-search-form form--no-dividers');

View File

@ -114,8 +114,8 @@ class GridFieldFilterHeaderTest extends SapphireTest
$this->assertEquals('field/testfield/schema/SearchForm', $searchSchema->formSchemaUrl);
$this->assertEquals('Name', $searchSchema->name);
$this->assertEquals('Search "Teams"', $searchSchema->placeholder);
$this->assertEquals('test', $searchSchema->filters->Name);
$this->assertEquals('place', $searchSchema->filters->City);
$this->assertEquals('test', $searchSchema->filters->Search__Name);
$this->assertEquals('place', $searchSchema->filters->Search__City);
$this->assertEquals('testfield', $searchSchema->gridfield);
}
}