F
Filament6mo ago
Lietze

filter form - reset filter A when filter B is some value

I'm trying to reset a filter when another filter is set to a specific value. so when has_lab_report is set back to false or show all, the results filter should be invisible again (which happens), but the filter is still active with the previously selected options (positive/negative etc) How can i reset the results filter when has_lab_reports is not Yes?
class LabReportResultFilter extends Filter
{
protected function setUp(): void
{
parent::setUp();

$this->label('Has result');
$this->form([
Grid::make()->schema([
Select::make('has_lab_report')
->placeholder('Show all')
->options([
'true' => 'Yes',
'false' => 'No',
])
// ->afterStateHydrated(function ($state, Set $set, Get $get) {
// if ($state) {
// $set('results', []);
// }
// })
,
// TODO reset 'results' when 'has_lab_report' is not 'true' anymore

Select::make('results')
->placeholder('Select a result')
->visible(fn (Get $get) => $get('has_lab_report') == 'true')
->multiple()
->options([
'positive' => 'Positive',
'negative' => 'Negative',
'dubious' => 'Dubious',
]),
]),
]);
// $this->query etc etc
class LabReportResultFilter extends Filter
{
protected function setUp(): void
{
parent::setUp();

$this->label('Has result');
$this->form([
Grid::make()->schema([
Select::make('has_lab_report')
->placeholder('Show all')
->options([
'true' => 'Yes',
'false' => 'No',
])
// ->afterStateHydrated(function ($state, Set $set, Get $get) {
// if ($state) {
// $set('results', []);
// }
// })
,
// TODO reset 'results' when 'has_lab_report' is not 'true' anymore

Select::make('results')
->placeholder('Select a result')
->visible(fn (Get $get) => $get('has_lab_report') == 'true')
->multiple()
->options([
'positive' => 'Positive',
'negative' => 'Negative',
'dubious' => 'Dubious',
]),
]),
]);
// $this->query etc etc
1 Reply
Lietze
Lietze6mo ago
Solution: on the first select "has_lab_report", add this:
->live()
->afterStateUpdated(fn ($state, Set $set) => $state !== '1' ? $set('results', []) : null),
->live()
->afterStateUpdated(fn ($state, Set $set) => $state !== '1' ? $set('results', []) : null),
(also changed the 'true' to an actual boolean true, not sure why i had that in the first place lol)