Skip to content

Commit

Permalink
Merge pull request #160 from dereuromark/master
Browse files Browse the repository at this point in the history
Allow emptyFields for PRG component in order for isSearch to stay correct
  • Loading branch information
ADmad committed Apr 25, 2017
2 parents 7eb3725 + 37400d8 commit 32d69ca
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -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.
Expand Down
15 changes: 14 additions & 1 deletion src/Controller/Component/PrgComponent.php
Expand Up @@ -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' => [],
];

/**
Expand Down Expand Up @@ -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;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase/Controller/Component/PrgComponentTest.php
Expand Up @@ -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
*/
Expand Down

0 comments on commit 32d69ca

Please sign in to comment.