afterStateHydrated not working when using multiple Fieldset

i'm trying to use afterStateHydrated on FieldA in Fieldset1 to update FieldB in Fieldset2, this does not work. When doing the same when FieldA and FieldB are in the same Fieldset1, then it works fine.
Solution:
``` Select::make('account_deal_id') ... ->live() ->afterStateUpdated(...
Jump to solution
18 Replies
LeandroFerreira
LeandroFerreira5mo ago
share some code you are trying please
johnnie_littleshoes
same issue here: country_id is being set to null, but id and status are not.
Select::make('country_id')
->relationship(
name: 'country',
titleAttribute: 'name',
modifyQueryUsing: fn (Builder $query) => $query->has('websites')
),
Select::make('account_deal_id')
->relationship('accountDeal', 'id')
->afterStateUpdated(
function ($state, Set $set) {
$set('country_id', null); // works
$set('id', null); // does not work
$set('status', null); // does not work
}
),

Fieldset::make('Deal Info')
->relationship('accountDeal')
->schema([
Select::make('id')
->relationship('account')
->getOptionLabelFromRecordUsing(fn ($record) => $record->getLabel())
->required(),
Select::make('status')
->label('Status')
->options(AccountDealStatus::class)
->required(),
]),
Select::make('country_id')
->relationship(
name: 'country',
titleAttribute: 'name',
modifyQueryUsing: fn (Builder $query) => $query->has('websites')
),
Select::make('account_deal_id')
->relationship('accountDeal', 'id')
->afterStateUpdated(
function ($state, Set $set) {
$set('country_id', null); // works
$set('id', null); // does not work
$set('status', null); // does not work
}
),

Fieldset::make('Deal Info')
->relationship('accountDeal')
->schema([
Select::make('id')
->relationship('account')
->getOptionLabelFromRecordUsing(fn ($record) => $record->getLabel())
->required(),
Select::make('status')
->label('Status')
->options(AccountDealStatus::class)
->required(),
]),
toeknee
toeknee5mo ago
->live() for it to trigger afterState
johnnie_littleshoes
I'm sorry, ->live() is already in the code, I accidentally deleted it when formatting it here
johnnie_littleshoes
I've also tried some "hacks", like ->live(debounce:150) ->native(false) ->searchable() as suggested by Dan here (https://github.com/filamentphp/filament/issues/8368) but to no avail
GitHub
$set in afterStateUpdated on select with scoped relationship not wo...
Package filament/filament Package Version 3.0.42 Laravel Version 10.22.0 Livewire Version No response PHP Version 8.2.9 Problem description I'm trying to set the property select to the customer...
toeknee
toeknee5mo ago
You can'd do it like that, I think you need to name the fieldset and sub set them to null. Since you are going into the fieldset as a relationship. if you dd ($get()) check what you get
johnnie_littleshoes
first of all, thank you guys so much for the prompt replies, you're real heroes! so I've renamed the Fieldset to 'deal_info'
dd($get('deal_info')); // returns null
dd($get('country_id)); // returns correct country_id
dd($get('id')); // returns Parent resource ID
dd($get('status')); // returns null
dd($get('deal_info.status')); // returns null
dd($get('deal_info')); // returns null
dd($get('country_id)); // returns correct country_id
dd($get('id')); // returns Parent resource ID
dd($get('status')); // returns null
dd($get('deal_info.status')); // returns null
as a clarification, this is in a Relation Manager
toeknee
toeknee5mo ago
Where are you doing the get?
johnnie_littleshoes
Select::make('account_deal_id')
->afterStateUpdated(
function ($state, Set $set, Get $get) {
$accountDeal = AccountDeal::find($state);
//dd($get('country_id'));
//dd($get('id'));
//dd($get('status')); null
//dd($get('deal_info')); null
//dd($get('deal_info.id')); null
//dd($get('deal_info.status')); null
//dd($get('deal_info[0]')); null
//dd($get('deal_info->status')); null
//dd($get('deal_info->id')); null
}
)
Select::make('account_deal_id')
->afterStateUpdated(
function ($state, Set $set, Get $get) {
$accountDeal = AccountDeal::find($state);
//dd($get('country_id'));
//dd($get('id'));
//dd($get('status')); null
//dd($get('deal_info')); null
//dd($get('deal_info.id')); null
//dd($get('deal_info.status')); null
//dd($get('deal_info[0]')); null
//dd($get('deal_info->status')); null
//dd($get('deal_info->id')); null
}
)
toeknee
toeknee5mo ago
Is that select a relationship? If so traverse out? ../
johnnie_littleshoes
dd($get('../deal_info')); dd($get('../deal_info.status')); both return null
toeknee
toeknee5mo ago
account_deal_id
johnnie_littleshoes
then I get the correct id from what I just selected, equal to $state Hey, wow, I got it I think you were on the right track. It wasn't about targetting the name of the Fieldset, but the relationship(?)
toeknee
toeknee5mo ago
Yeah along those lines, it's tricky to work it out when inbetween snippets of code
Solution
johnnie_littleshoes
Select::make('account_deal_id')
...
->live()
->afterStateUpdated(
function ($state, Set $set, Get $get) {
dd(
$get('deal_info'), null
$get('accountDeal'), related record OK
$get('accountDeal.id'), account_id OK
$get('accountDeal.status'), status OK

);
}
)
...

Fieldset::make('deal_info')
->relationship('accountDeal')
->schema([
Select::make('id')
->relationship('account')
...

Select::make('status')
...
]),
Select::make('account_deal_id')
...
->live()
->afterStateUpdated(
function ($state, Set $set, Get $get) {
dd(
$get('deal_info'), null
$get('accountDeal'), related record OK
$get('accountDeal.id'), account_id OK
$get('accountDeal.status'), status OK

);
}
)
...

Fieldset::make('deal_info')
->relationship('accountDeal')
->schema([
Select::make('id')
->relationship('account')
...

Select::make('status')
...
]),
johnnie_littleshoes
so I can basically call the Fieldset anything really, but then $get its fields through the ->relationship
toeknee
toeknee5mo ago
Yes that makes sense if you did not define the relationship it then groups it onto the fieldset name
johnnie_littleshoes
Ahhh I see, makes sense Thanks a lot for the support, toeknee!
Want results from more Discord servers?
Add your server
More Posts