Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Screen/Fields/Support/ChoicePayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ private static function normalizeScope(mixed $scope): ?array
*/
private function selectedItems(iterable $keys): iterable
{
$keys = collect($keys)->filter(fn ($key): bool => $key !== null && $key !== '')->values();

if ($keys->isEmpty()) {
return [];
}

$query = $this->query();

return $query instanceof Builder
Expand Down
42 changes: 42 additions & 0 deletions tests/Unit/Screen/Fields/ChoicePayloadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Orchid\Tests\Unit\Screen\Fields;

use Illuminate\Support\Facades\DB;
use Orchid\Platform\Models\Role;
use Orchid\Screen\Fields\Support\ChoicePayload;
use Orchid\Tests\Unit\Screen\TestFieldsUnitCase;

/**
* Class ChoicePayloadTest.
*/
class ChoicePayloadTest extends TestFieldsUnitCase
{
public function testSelectedOptionsIgnoresBlankValuesWithoutQuerying(): void
{
$payload = new ChoicePayload(model: Role::class, name: 'name', key: 'id');

DB::enableQueryLog();

$options = $payload->selectedOptions('');

$this->assertSame([], $options);
$this->assertEmpty(DB::getQueryLog());

DB::disableQueryLog();
}

public function testSelectedOptionsIgnoresNullAmongSelectedValues(): void
{
$role = Role::factory()->create();

$payload = new ChoicePayload(model: Role::class, name: 'name', key: 'id');

$options = $payload->selectedOptions([$role->id, null, '']);

$this->assertCount(1, $options);
$this->assertSame($role->id, $options[0]['id']);
}
}
Loading