Hide and set value don't work simultaneously

I have a Form with two fields, redeemer_type and redeemer_id and I want to set the redeemer_id to NULL and hide it when the redeemer_type is null. My code works fine if I comment the hidden part of the redeemer_id, any ideas?
Select::make('redeemer_type')
->label(__('Type'))
->options([
User::class => __('User'),
])
->afterStateUpdated(fn (Get $get, Set $set) => $set('redeemer_id', null))
->live(),

Select::make('redeemer_id')
->label(__('User'))
->getSearchResultsUsing(fn (string $search): array => User::query()
->where('name', 'like', "%{$search}%")
->where('is_admin', false)
->limit(10)
->pluck('name', 'id')
->toArray()
)
->getOptionLabelUsing(fn ($value): ?string => User::find($value)?->name)
// ->hidden(fn (Get $get): bool => $get('redeemer_type') !== User::class)
->searchable(),
Select::make('redeemer_type')
->label(__('Type'))
->options([
User::class => __('User'),
])
->afterStateUpdated(fn (Get $get, Set $set) => $set('redeemer_id', null))
->live(),

Select::make('redeemer_id')
->label(__('User'))
->getSearchResultsUsing(fn (string $search): array => User::query()
->where('name', 'like', "%{$search}%")
->where('is_admin', false)
->limit(10)
->pluck('name', 'id')
->toArray()
)
->getOptionLabelUsing(fn ($value): ?string => User::find($value)?->name)
// ->hidden(fn (Get $get): bool => $get('redeemer_type') !== User::class)
->searchable(),
Solution:
I think your code should work. Could you try this? ```php Select::make('redeemer_type') ->label(__('Type'))...
Jump to solution
4 Replies
LeandroFerreira
LeandroFerreira6mo ago
what is the error?
Roberto S.
Roberto S.OP6mo ago
The error is that if I hide the redeem_id when the redeemer_type is NULL it is not set to NULL. However if I don't hide it the behaviour is correct.
Solution
LeandroFerreira
LeandroFerreira6mo ago
I think your code should work. Could you try this?
Select::make('redeemer_type')
->label(__('Type'))
->options([
User::class => __('User'),
])
->live()
->afterStateUpdated(function (?string $state, Select $component) {
$redeemer = $component->getContainer()->getComponent('data.redeemer_id');

if (blank($state)) {
$redeemer->state(null);
$redeemer->hidden();
}
})
Select::make('redeemer_type')
->label(__('Type'))
->options([
User::class => __('User'),
])
->live()
->afterStateUpdated(function (?string $state, Select $component) {
$redeemer = $component->getContainer()->getComponent('data.redeemer_id');

if (blank($state)) {
$redeemer->state(null);
$redeemer->hidden();
}
})
Roberto S.
Roberto S.OP6mo ago
Awesome! Thanks a lot. It works great
Want results from more Discord servers?
Add your server