-
Notifications
You must be signed in to change notification settings - Fork 62
Add formClass option to the SearchComponent.
#354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
48e8561
Add `formClass` option to the SearchComponent.
ADmad e35b6c6
Add docs for the `formClass` option
ADmad 6922fb1
Add missing test class
ADmad 20c034b
Update phpcs config
ADmad 00e471a
Fix CS error
ADmad 5f39dd1
Remove deprecated no-op call
ADmad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,11 @@ | |
|
|
||
| use Cake\Controller\Component; | ||
| use Cake\Controller\ComponentRegistry; | ||
| use Cake\Core\App; | ||
| use Cake\Core\Configure; | ||
| use Cake\Core\Exception\CakeException; | ||
| use Cake\Event\EventInterface; | ||
| use Cake\Form\Form; | ||
| use Cake\Utility\Hash; | ||
| use Closure; | ||
| use UnexpectedValueException; | ||
|
|
@@ -35,6 +38,7 @@ class SearchComponent extends Component | |
| * - `modelClass` : Configure the controller's modelClass to be used for the query, used to | ||
| * populate the _isSearch view variable to allow for a reset button, for example. | ||
| * Set to false to disable the auto-setting of the view variable. | ||
| * - `formClass` : The form class to use for the search form. Default `null`. | ||
| * - `events`: List of events this component listens to. You can disable an | ||
| * event by setting it to false. | ||
| * E.g. `'events' => ['Controller.beforeRender' => false]` | ||
|
|
@@ -47,6 +51,7 @@ class SearchComponent extends Component | |
| 'queryStringBlacklist' => ['_csrfToken', '_Token'], | ||
| 'emptyValues' => [], | ||
| 'modelClass' => null, | ||
| 'formClass' => null, | ||
| 'events' => [ | ||
| 'Controller.startup' => 'startup', | ||
| 'Controller.beforeRender' => 'beforeRender', | ||
|
|
@@ -83,13 +88,30 @@ public function implementedEvents(): array | |
| */ | ||
| public function startup(EventInterface $event): void | ||
| { | ||
| if (!$this->getController()->getRequest()->is('post') || !$this->_isSearchAction()) { | ||
| if (!$this->_isSearchAction()) { | ||
| return; | ||
| } | ||
|
|
||
| $url = $this->getController()->getRequest()->getPath(); | ||
| $form = $this->getSearchForm(); | ||
|
|
||
| if (!$this->getController()->getRequest()->is('post')) { | ||
| if ($form !== null) { | ||
| $this->getController()->set('searchForm', $form); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $params = (array)$this->getController()->getRequest()->getData(); | ||
|
|
||
| if ($form !== null && !$form->validate($params)) { | ||
| $this->getController()->set('searchForm', $form); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $params = $this->_filterParams(); | ||
| $url = $this->getController()->getRequest()->getPath(); | ||
| $params = $this->_filterParams($params); | ||
| if ($params) { | ||
| $params = Hash::expand($params); | ||
| $url .= '?' . http_build_query($params); | ||
|
|
@@ -98,6 +120,28 @@ public function startup(EventInterface $event): void | |
| $event->setResult($this->_registry->getController()->redirect($url)); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the search form instance if a `formClass` has been configured. | ||
| * | ||
| * @throws \Cake\Core\Exception\CakeException When the configured form does not exist. | ||
| * @return \Cake\Form\Form|null | ||
| */ | ||
| protected function getSearchForm(): ?Form | ||
| { | ||
| $fc = $this->getConfig('formClass'); | ||
| if (!is_string($fc)) { | ||
| return null; | ||
| } | ||
|
|
||
| /** @var class-string<\Cake\Form\Form>|null $formClass */ | ||
| $formClass = App::className($fc, 'Form', 'Form'); | ||
| if ($formClass === null) { | ||
| throw new CakeException(sprintf('The form class `%s` could not be found.', $fc)); | ||
| } | ||
|
|
||
| return new $formClass(); | ||
| } | ||
|
|
||
| /** | ||
| * Populates the $_isSearch view variable based on the current request. | ||
| * | ||
|
|
@@ -147,11 +191,12 @@ protected function _isSearchAction(): bool | |
| /** | ||
| * Filters the params from POST data and merges in the whitelisted query string ones. | ||
| * | ||
| * @param array $params The params to filter. | ||
| * @return array | ||
| */ | ||
| protected function _filterParams(): array | ||
| protected function _filterParams(array $params): array | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dereuromark This is a breaking signature change but I think it's acceptable for a protected method in a new minor release. |
||
| { | ||
| $params = Hash::filter((array)$this->getController()->getRequest()->getData()); | ||
| $params = Hash::filter($params); | ||
|
|
||
| foreach ((array)$this->getConfig('queryStringBlacklist') as $field) { | ||
| unset($params[$field]); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Search\Test\TestApp\Form; | ||
|
|
||
| use Cake\Form\Form; | ||
| use Cake\Validation\Validator; | ||
|
|
||
| class SearchForm extends Form | ||
| { | ||
| public function validationDefault(Validator $validator): Validator | ||
| { | ||
| $validator | ||
| ->allowEmptyString('q') | ||
| ->minLength('q', 3, 'Search query must be at least 3 characters long'); | ||
|
|
||
| return $validator; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.