Skip to content

Commit

Permalink
Add empty option by default for search form selects.
Browse files Browse the repository at this point in the history
Closes #316
  • Loading branch information
ADmad committed Apr 21, 2023
1 parent 2a35f2c commit ea225ca
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
7 changes: 6 additions & 1 deletion src/Listener/ViewSearchListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand Down
11 changes: 6 additions & 5 deletions tests/Fixture/BlogsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
];
}
42 changes: 37 additions & 5 deletions tests/TestCase/Listener/ViewSearchListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit ea225ca

Please sign in to comment.