Skip to content
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

Grouping options in Select field breaks validation rule in #15036

Open
standaniels opened this issue Dec 9, 2024 · 0 comments
Open

Grouping options in Select field breaks validation rule in #15036

standaniels opened this issue Dec 9, 2024 · 0 comments
Labels
Milestone

Comments

@standaniels
Copy link
Contributor

standaniels commented Dec 9, 2024

Package

filament/filament

Package Version

v3.2.128

Laravel Version

v11.34.2

Livewire Version

v3.5.12

PHP Version

8.3.12

Problem description

When using a Select, and passing an array of groups to options(), numeric keys (representing database IDs) are discarded when getting the enabled options via $component->getEnabledOptions().

public function getEnabledOptions(): array
{
    return collect($this->getOptions())
        ->reduce(function (Collection $carry, $label, $value): Collection {
            if (is_array($label)) {
                return $carry->merge($label); // <= this discards numeric keys
            }

            return $carry->put($value, $label);
        }, collect())
        ->filter(fn ($label, $value) => ! $this->isOptionDisabled($value, $label))
        ->all();
}

Expected behavior

Numeric keys are kept, flattening the options array like so:

public function getEnabledOptions(): array
{
    return collect($this->getOptions())
        ->reduce(function (Collection $carry, $label, $value): Collection {
            if (is_iterable($label)) {
                foreach ($label as $k => $v) {
                    $carry->put($k, $v);
                }

                return $carry;
            }

            return $carry->put($value, $label);
        }, collect())
        ->filter(fn ($label, $value) => ! $this->isOptionDisabled($value, $label))
        ->all();
}

Steps to reproduce

Having the following schema:

return $form->schema([
    Forms\Components\Select::make('foobar')
        ->options([
            'Foo' => [
                10 => 'Option 10',
            ],
            'Bar' => [
                20 => 'Option 20',
            ],
        ])
        ->in(function (Select $component): array {
            dd($component->getEnabledOptions());
        })
        ->required(),
]);

This is the result of the dd:

array:2 [▼ // app/Filament/Resources/PageResource.php:37
  0 => "Option 10"
  1 => "Option 20"
]

While I expected this result:

array:2 [▼ // app/Filament/Resources/PageResource.php:35
  10 => "Option 10"
  20 => "Option 20"
]

Reproduction repository (issue will be closed if this is not valid)

https://github.com/standaniels/filament-bug

Relevant log output

No response

Donate 💰 to fund this issue

  • You can donate funding to this issue. We receive the money once the issue is completed & confirmed by you.
  • 100% of the funding will be distributed between the Filament core team to run all aspects of the project.
  • Thank you in advance for helping us make maintenance sustainable!
Fund with Polar
@github-project-automation github-project-automation bot moved this to Todo in Roadmap Dec 9, 2024
@danharrin danharrin added this to the v3 milestone Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Todo
Development

No branches or pull requests

2 participants