F
Filament7mo ago
Gudao

Clearing a dependent Select field when the field that it is depending changes value

I need help in finding a way to clear or dehydrate the 'name' Select Field whenever the LOB Select Field changes Select::make('LOB')->label('LOB') ->options($departmentLabels) ->live(), Select::make('name')->label('Name')->searchable() ->options(function (callable $get) use ($groupedUsers) { $lob = $get('LOB'); return $groupedUsers($lob) ?? []; }),
so far ive tried the method ->afterStateUpdated() but the problem there is any other field that changes also clears the name field and I can really use a conditional in the method to check if the old state != new state
Solution:
```php use Livewire\Component as Livewire; ->afterStateUpdated(function (Livewire $livewire) { $livewire->reset('data.name');...
Jump to solution
23 Replies
Povilas K
Povilas K7mo ago
@Gudao code example from our project with dependent dropdowns:
Forms\Components\Select::make('city_id')
->required()
->label('City')
->placeholder(fn (Forms\Get $get): string => empty($get('country_id')) ? 'First select country' : 'Select an option')
->options(function (?Shop $record, Forms\Get $get, Forms\Set $set) {
if (! empty($record) && empty($get('country_id'))) {
$set('country_id', $record->city->country_id);
$set('city_id', $record->city_id);
}

return City::where('country_id', $get('country_id'))->pluck('name', 'id');
}),
Forms\Components\Select::make('city_id')
->required()
->label('City')
->placeholder(fn (Forms\Get $get): string => empty($get('country_id')) ? 'First select country' : 'Select an option')
->options(function (?Shop $record, Forms\Get $get, Forms\Set $set) {
if (! empty($record) && empty($get('country_id'))) {
$set('country_id', $record->city->country_id);
$set('city_id', $record->city_id);
}

return City::where('country_id', $get('country_id'))->pluck('name', 'id');
}),
So maybe in your case you need to call $set('name', '') or similar inside of your callable?
Gudao
GudaoOP7mo ago
thats what I did in my ->afterStateUpdated() when LOB updates, I set 'name' to null tho I found out that it wasnt just LOB that triggered that method Other fields that had ->live() also triggered it somehow
Povilas K
Povilas K7mo ago
but can't you do the same in the options() instead of afterStateUpdated(), like in my example? Can't guarantee it would work, but it worked in our case
Gudao
GudaoOP7mo ago
sure Ill try ill get back to you nope didnt work
Gudao
GudaoOP7mo ago
No description
Gudao
GudaoOP7mo ago
heres more context So the LOB is basically the department Employee 1 is under Call Entering but when I changed it to Emergency Repair Group but It didnt clear employee 1 out of the select
krekas
krekas7mo ago
I don't see what you have tried in the afterStateUpdated.
Gudao
GudaoOP7mo ago
wait let me show you the exact method I deleted it since it didnt work as intended
krekas
krekas7mo ago
And callable $get? Where those variables even come from for the options? In my opinion whole code is bad
Gudao
GudaoOP7mo ago
callable $get is used for a custom query depending on LOB
Gudao
GudaoOP7mo ago
->afterStateUpdated(function ($get, $set, $state) { $set('name', null); // Dehydrate name }),
krekas
krekas7mo ago
I don't understand where callable comes from
Solution
krekas
krekas7mo ago
use Livewire\Component as Livewire;

->afterStateUpdated(function (Livewire $livewire) {
$livewire->reset('data.name');
}),
use Livewire\Component as Livewire;

->afterStateUpdated(function (Livewire $livewire) {
$livewire->reset('data.name');
}),
Gudao
GudaoOP7mo ago
dunno anymore ive been at it for like half my shift Ill try that rn
krekas
krekas7mo ago
even in v2 $get wasn't callable, it was closure
Gudao
GudaoOP7mo ago
tho its still worked dunno why
krekas
krekas7mo ago
you are doing something weird here
Gudao
GudaoOP7mo ago
I tried this name field start clearing itself as soon as I updated it
krekas
krekas7mo ago
but you want that. after lob is selected clear the name...
Gudao
GudaoOP7mo ago
i mean if LOB gets updated like if I was user I selected the wrong LOB and I selected the name I would change the LOB and to avoid error clear name when it was updated
krekas
krekas7mo ago
so you add afterStateUpdated to the lob field if needed do some if checks
Gudao
GudaoOP7mo ago
ah would this work as well If I chained it nature of error depends on Error Category which depends on LOB but yeah now It works thanks @krekas also thank you for pointing out my code id probably do some cleaning
Want results from more Discord servers?
Add your server