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
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:Jump to solution
```php
use Livewire\Component as Livewire;
->afterStateUpdated(function (Livewire $livewire) {
$livewire->reset('data.name');...
23 Replies
@Gudao code example from our project with dependent dropdowns:
So maybe in your case you need to call
$set('name', '')
or similar inside of your callable?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
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
sure Ill try
ill get back to you
nope didnt work
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
I don't see what you have tried in the afterStateUpdated.
wait let me show you the exact method
I deleted it since it didnt work as intended
And callable $get?
Where those variables even come from for the options? In my opinion whole code is bad
callable $get is used for a custom query depending on LOB
->afterStateUpdated(function ($get, $set, $state) {
$set('name', null); // Dehydrate name
}),
I don't understand where callable comes from
Solution
dunno anymore ive been at it for like half my shift
Ill try that rn
even in v2 $get wasn't callable, it was closure
tho its still worked dunno why
you are doing something weird here
I tried this
name field start clearing itself as soon as I updated it
but you want that. after lob is selected clear the name...
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
so you add afterStateUpdated to the lob field
if needed do some if checks
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