From ea225ca8fa56929b455f2dba2afae89fd24120f9 Mon Sep 17 00:00:00 2001 From: ADmad Date: Mon, 17 Apr 2023 18:37:44 +0530 Subject: [PATCH] Add empty option by default for search form selects. Closes #316 --- src/Listener/ViewSearchListener.php | 7 +++- tests/Fixture/BlogsFixture.php | 11 ++--- .../Listener/ViewSearchListenerTest.php | 42 ++++++++++++++++--- 3 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/Listener/ViewSearchListener.php b/src/Listener/ViewSearchListener.php index 4efca6d..1575e65 100644 --- a/src/Listener/ViewSearchListener.php +++ b/src/Listener/ViewSearchListener.php @@ -115,10 +115,15 @@ public function fields(): array $input['value'] = $request->getQuery($field); if (empty($input['options']) && $schema->getColumnType($field) === 'boolean') { - $input['options'] = ['No', 'Yes']; + $input['options'] = [__d('crud', 'No'), __d('crud', 'Yes')]; $input['type'] = 'select'; } + /** @psalm-suppress PossiblyUndefinedArrayOffset */ + if ($input['type'] === 'select') { + $input += ['empty' => true]; + } + if (!empty($input['options'])) { $input['empty'] = true; if (empty($input['class']) && !$config['select2']) { diff --git a/tests/Fixture/BlogsFixture.php b/tests/Fixture/BlogsFixture.php index 4a677fb..fa6da5e 100644 --- a/tests/Fixture/BlogsFixture.php +++ b/tests/Fixture/BlogsFixture.php @@ -12,14 +12,15 @@ class BlogsFixture extends TestFixture 'is_active' => ['type' => 'boolean', 'default' => true, 'null' => false], 'name' => ['type' => 'string', 'length' => 255, 'null' => false], 'body' => ['type' => 'text', 'null' => false], + 'user_id' => ['type' => 'integer'], '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]], ]; public $records = [ - ['name' => '1st post', 'body' => '1st post body'], - ['name' => '2nd post', 'body' => '2nd post body'], - ['name' => '3rd post', 'body' => '3rd post body'], - ['name' => '4th post', 'body' => '4th post body'], - ['name' => '5th post', 'body' => '5th post body'], + ['name' => '1st post', 'body' => '1st post body', 'user_id' => 1], + ['name' => '2nd post', 'body' => '2nd post body', 'user_id' => 1], + ['name' => '3rd post', 'body' => '3rd post body', 'user_id' => 1], + ['name' => '4th post', 'body' => '4th post body', 'user_id' => 1], + ['name' => '5th post', 'body' => '5th post body', 'user_id' => 1], ]; } diff --git a/tests/TestCase/Listener/ViewSearchListenerTest.php b/tests/TestCase/Listener/ViewSearchListenerTest.php index 696cd72..8b3c905 100644 --- a/tests/TestCase/Listener/ViewSearchListenerTest.php +++ b/tests/TestCase/Listener/ViewSearchListenerTest.php @@ -48,26 +48,58 @@ public function setUp(): void public function testFields() { - $this->listener->setConfig(['fields' => ['category_id']]); + $this->listener->setConfig(['fields' => [ + 'name', + 'is_active', + 'user_id', + 'custom_select' => ['empty' => false, 'type' => 'select'], + ]]); $fields = $this->listener->fields(); $expected = [ - 'category_id' => [ + 'name' => [ 'required' => false, 'type' => 'select', 'value' => null, 'class' => 'autocomplete', - 'data-url' => '/blogs/lookup.json?id=category_id&value=category_id', + 'data-url' => '/blogs/lookup.json?id=name&value=name', + 'data-input-type' => 'text', + 'data-tags' => 'true', + 'data-allow-clear' => 'true', + 'data-placeholder' => '', + ], + 'is_active' => [ + 'required' => false, + 'type' => 'select', + 'value' => null, + 'empty' => true, + 'options' => ['No', 'Yes'], + ], + 'user_id' => [ + 'required' => false, + 'type' => 'select', + 'empty' => true, + 'value' => null, + 'class' => 'autocomplete', + 'data-url' => '/blogs/lookup.json?id=user_id&value=user_id', + ], + 'custom_select' => [ + 'required' => false, + 'type' => 'select', + 'empty' => false, + 'value' => null, + 'class' => 'autocomplete', + 'data-url' => '/blogs/lookup.json?id=custom_select&value=custom_select', ], ]; $this->assertEquals($expected, $fields); $this->listener->setConfig([ - 'fields' => ['category_id' => ['data-url' => '/custom']], + 'fields' => ['name' => ['data-url' => '/custom']], ], null, true); $fields = $this->listener->fields(); - $expected['category_id']['data-url'] = '/custom'; + $expected['name']['data-url'] = '/custom'; $this->assertEquals($expected, $fields); } }