F
Filamentā€¢10mo ago
CT

->disabled() on dependant Select broken on create page, works fine on edit modal.

I have the following two Selects, Contact is a dependant select based upon the Company selected. The below code works perfectly on an edit modal, but doesn't work on the create page. On the create page it starts as disabled, then once I select a Company, it becomes clickable but nothing happens (not even the usual "type ... to search" appears. No console errors and I can see in Clockwork that the requests to the database look correct.
Forms\Components\Select::make('company_id')
->live(onBlur: true)
->required()
->label('Company')
->preload()
->searchable()
->relationship(name: 'company', titleAttribute: 'name')
->afterStateUpdated(function (Forms\Contracts\HasForms $livewire, Forms\Set $set) {
// If we change the company make sure we null the contact to prevent errors.
$set('contact_id', null);
}),

Forms\Components\Select::make('contact_id')
->label('Contact')
->searchable()
->disabled(fn(Forms\Get $get) => !filled($get('company_id'))) // If I disable this line everything works correctly on the create page also
->preload()
->getOptionLabelFromRecordUsing(fn(Contact $contact) => $contact->full_name)
->relationship(
name: 'contact',
titleAttribute: 'first_name',
modifyQueryUsing: fn(Builder $query, Forms\Get $get) => $query->where('contacts.company_id', $get('company_id'))
),
Forms\Components\Select::make('company_id')
->live(onBlur: true)
->required()
->label('Company')
->preload()
->searchable()
->relationship(name: 'company', titleAttribute: 'name')
->afterStateUpdated(function (Forms\Contracts\HasForms $livewire, Forms\Set $set) {
// If we change the company make sure we null the contact to prevent errors.
$set('contact_id', null);
}),

Forms\Components\Select::make('contact_id')
->label('Contact')
->searchable()
->disabled(fn(Forms\Get $get) => !filled($get('company_id'))) // If I disable this line everything works correctly on the create page also
->preload()
->getOptionLabelFromRecordUsing(fn(Contact $contact) => $contact->full_name)
->relationship(
name: 'contact',
titleAttribute: 'first_name',
modifyQueryUsing: fn(Builder $query, Forms\Get $get) => $query->where('contacts.company_id', $get('company_id'))
),
Anyone got any ideas?
4 Replies
Tieme
Tiemeā€¢10mo ago
If you move the ->disabled( to bottom what happens then? So below the ->relationship
CT
CTOPā€¢10mo ago
same as before unfortunately šŸ˜¦ here is a comparison of the two fields after selecting a company. Both start out as disabled, then the working on the left is the edit modal and the right is the create page
No description
No description
šŸ¤–transistoršŸ¤–
have you tried empty instead of filled ? !filled($get('company_id')) -> !empty($get('company_id'))
CT
CTOPā€¢9mo ago
yep, same behavior. I swapped it to filled after empty wasn't working
Want results from more Discord servers?
Add your server