mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
commit
c8e7672ede
@ -235,11 +235,19 @@ class GridFieldFilterHeader implements GridField_URLHandler, GridField_HTMLProvi
|
|||||||
|
|
||||||
$name = $gridField->Title ?: singleton($gridField->getModelClass())->i18n_plural_name();
|
$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 = [
|
$schema = [
|
||||||
'formSchemaUrl' => $schemaUrl,
|
'formSchemaUrl' => $schemaUrl,
|
||||||
'name' => $searchField,
|
'name' => $searchField,
|
||||||
'placeholder' => _t(__CLASS__ . '.Search', 'Search "{name}"', ['name' => $name]),
|
'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(),
|
'gridfield' => $gridField->getName(),
|
||||||
'searchAction' => GridField_FormAction::create($gridField, 'filter', false, 'filter', null)->getAttribute('name'),
|
'searchAction' => GridField_FormAction::create($gridField, 'filter', false, 'filter', null)->getAttribute('name'),
|
||||||
'clearAction' => GridField_FormAction::create($gridField, 'reset', false, 'reset', 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);
|
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();
|
$columns = $gridField->getColumns();
|
||||||
|
|
||||||
// Update field titles to match column titles
|
// Update field titles to match column titles
|
||||||
@ -289,6 +302,7 @@ class GridFieldFilterHeader implements GridField_URLHandler, GridField_HTMLProvi
|
|||||||
$searchFields,
|
$searchFields,
|
||||||
new FieldList()
|
new FieldList()
|
||||||
);
|
);
|
||||||
|
|
||||||
$form->setFormMethod('get');
|
$form->setFormMethod('get');
|
||||||
$form->setFormAction($gridField->Link());
|
$form->setFormAction($gridField->Link());
|
||||||
$form->addExtraClass('cms-search-form form--no-dividers');
|
$form->addExtraClass('cms-search-form form--no-dividers');
|
||||||
|
@ -114,8 +114,8 @@ class GridFieldFilterHeaderTest extends SapphireTest
|
|||||||
$this->assertEquals('field/testfield/schema/SearchForm', $searchSchema->formSchemaUrl);
|
$this->assertEquals('field/testfield/schema/SearchForm', $searchSchema->formSchemaUrl);
|
||||||
$this->assertEquals('Name', $searchSchema->name);
|
$this->assertEquals('Name', $searchSchema->name);
|
||||||
$this->assertEquals('Search "Teams"', $searchSchema->placeholder);
|
$this->assertEquals('Search "Teams"', $searchSchema->placeholder);
|
||||||
$this->assertEquals('test', $searchSchema->filters->Name);
|
$this->assertEquals('test', $searchSchema->filters->Search__Name);
|
||||||
$this->assertEquals('place', $searchSchema->filters->City);
|
$this->assertEquals('place', $searchSchema->filters->Search__City);
|
||||||
$this->assertEquals('testfield', $searchSchema->gridfield);
|
$this->assertEquals('testfield', $searchSchema->gridfield);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user