From 37400d8e72128eabc33e9bfb9423ab0474a291bc Mon Sep 17 00:00:00 2001 From: dereuromark Date: Sat, 22 Apr 2017 00:25:32 +0200 Subject: [PATCH] Allow emptyFields for PRG component in order for isSearch to stay correct. --- README.md | 14 +++++++++++++ src/Controller/Component/PrgComponent.php | 15 ++++++++++++- .../Controller/Component/PrgComponentTest.php | 21 +++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3012788d..d9f83f48 100644 --- a/README.md +++ b/README.md @@ -375,6 +375,20 @@ Be sure to allow empty in your search form, if you're using one. echo $this->Form->input('author_id', ['empty' => 'Pick an author']); ``` +## Empty fields +In some cases, e.g. when posting checkboxes, the empty value is not `''` but `'0'`. +If you want to declare certain values as empty values and prevent the URL of getting the query string attached for this "disabled" search field, you can set `emptyValues` in the component: +```php + $this->loadComponent('Search.Prg', [ + ... + 'emptyValues' => [ + 'my_checkbox' => '0', + ] + ]); +``` + +This is needed for the "isSearch" work as expected. + ## Persisting the Query String Persisting the query string can be done with the `queryStringWhitelist` option. diff --git a/src/Controller/Component/PrgComponent.php b/src/Controller/Component/PrgComponent.php index 7192c032..e6fb0c37 100644 --- a/src/Controller/Component/PrgComponent.php +++ b/src/Controller/Component/PrgComponent.php @@ -18,13 +18,16 @@ class PrgComponent extends Component * - `queryStringToData` : Set query string as request data. Default `true`. * - `queryStringWhitelist` : An array of whitelisted query strings to be kept. * Defaults to the Paginator `'sort'`, `'direction'` and `'limit'` ones. + * - `emptyValues` : A map of fields and their values to be considered empty + * (will not be passed along in the URL). * * @var array */ protected $_defaultConfig = [ 'actions' => ['index', 'lookup'], 'queryStringToData' => true, - 'queryStringWhitelist' => ['sort', 'direction', 'limit'] + 'queryStringWhitelist' => ['sort', 'direction', 'limit'], + 'emptyValues' => [], ]; /** @@ -96,6 +99,16 @@ protected function _filterParams() { $params = Hash::filter($this->request->data); + foreach ((array)$this->config('emptyValues') as $field => $value) { + if (!isset($params[$field])) { + continue; + } + + if ($params[$field] === (string)$value) { + unset($params[$field]); + } + } + if (!$this->config('queryStringWhitelist')) { return $params; } diff --git a/tests/TestCase/Controller/Component/PrgComponentTest.php b/tests/TestCase/Controller/Component/PrgComponentTest.php index 1570ed5a..c48eb704 100644 --- a/tests/TestCase/Controller/Component/PrgComponentTest.php +++ b/tests/TestCase/Controller/Component/PrgComponentTest.php @@ -118,6 +118,27 @@ public function testInitializePost() $this->assertEquals('http://localhost/users/my-predictions', $response->header()['Location']); } + /** + * @return void + */ + public function testInitializePostWithEmptyValues() + { + $this->Controller->request->params = [ + 'controller' => 'Posts', + 'action' => 'index', + 'pass' => ['pass'] + ]; + $this->Controller->request->here = '/Posts/index/pass'; + $this->Controller->request->data = ['foo' => 'bar', 'checkbox' => '0']; + $this->Controller->request->env('REQUEST_METHOD', 'POST'); + + $this->Prg->configShallow('emptyValues', [ + 'checkbox' => '0', + ]); + $response = $this->Prg->startup(); + $this->assertEquals('http://localhost/Posts/index/pass?foo=bar', $response->header()['Location']); + } + /** * @return void */