Filter a Select Field in a Repeater

How to filter a Select Field in a Repeater to avoid duplicated?
4 Replies
LeandroFerreira
For real-time validation, you can try something like this:
Repeater::make('repeater')
->schema([
Select::make('select')
->options([
1 => 'Option 1',
2 => 'Option 2',
3 => 'Option 3',
])
->reactive()
->afterStateUpdated(function ($component, $state, $livewire) {

$items = $component->getContainer()->getParentComponent()->getOldState();
$livewire->resetErrorBag($component->getStatePath());

if (in_array([$component->getName() => $state], $items)) {
$livewire->addError($component->getStatePath(), 'duplicated');
}
})
])
Repeater::make('repeater')
->schema([
Select::make('select')
->options([
1 => 'Option 1',
2 => 'Option 2',
3 => 'Option 3',
])
->reactive()
->afterStateUpdated(function ($component, $state, $livewire) {

$items = $component->getContainer()->getParentComponent()->getOldState();
$livewire->resetErrorBag($component->getStatePath());

if (in_array([$component->getName() => $state], $items)) {
$livewire->addError($component->getStatePath(), 'duplicated');
}
})
])
To prevent duplicate data, you can use hooks: https://filamentphp.com/docs/2.x/admin/resources/creating-records#lifecycle-hooks
Filament
Creating records - Resources - Admin Panel - Filament
The elegant TALL stack admin panel for Laravel artisans.
abdullafahem
abdullafahemOP2y ago
Thank you!
LeandroFerreira
Did it work?
abdullafahem
abdullafahemOP2y ago
What I was interested about is to filter items in Select Field so when an item is selected it will be removed from the list in Repeater items below.
Want results from more Discord servers?
Add your server