How to update field when is hidden

Hello, I have 2 select in a form. The second is only visible when a value of the first select has a understated. I'm having trouble when I update a status that includes a substate and change it to one that doesn't, the substate is not nulled on save in the database.
Forms\Components\Select::make('status_id')
->relationship('status', 'name', fn(Builder $query) => $query
->where('parent_id', 0)
->orderBy('order'))
->required()
->reactive()
->afterStateUpdated(fn ($set) => $set('understated_id', null))
->default(1)
->disabled(isCustomer()),

Forms\Components\Select::make('understated_id')
->options(function (\Closure $get) {
$status = Status::query()->where('parent_id', '=', $get('status_id'))
->orderBy('order')->get();
if ($status) {
return $status->where('parent_id', $get('status_id'))->pluck('name', 'id');
}
return [];
})
->visible(function (\Closure $get) {
if ($get('status_id') <> '' && Status::query()->where('parent_id', '=', $get('status_id'))->exists()) {
return true;
}
return false;
})
->required(),
Forms\Components\Select::make('status_id')
->relationship('status', 'name', fn(Builder $query) => $query
->where('parent_id', 0)
->orderBy('order'))
->required()
->reactive()
->afterStateUpdated(fn ($set) => $set('understated_id', null))
->default(1)
->disabled(isCustomer()),

Forms\Components\Select::make('understated_id')
->options(function (\Closure $get) {
$status = Status::query()->where('parent_id', '=', $get('status_id'))
->orderBy('order')->get();
if ($status) {
return $status->where('parent_id', $get('status_id'))->pluck('name', 'id');
}
return [];
})
->visible(function (\Closure $get) {
if ($get('status_id') <> '' && Status::query()->where('parent_id', '=', $get('status_id'))->exists()) {
return true;
}
return false;
})
->required(),
1 Reply
toeknee
toeknee2y ago
I would check the mutate before save and check the value, if the value is X convert to null and save.
Want results from more Discord servers?
Add your server