FilamentF
Filament2y ago
Lars

Update hidden fields (Relationship)

So I have a select input that is a relation. This field is hidden or shown based on the type of the user. But when type is changed to Customer the relation, the select, should be updated. I know that the field is not being updated because it is hidden. Is their away around this?

        return $form
            ->schema([
                Forms\Components\Group::make()->schema([
                    Forms\Components\Section::make()->schema([
                        Forms\Components\TextInput::make('name')
                            ->label('Name')
                            ->required()
                            ->placeholder('John Doe')
                            ->maxLength(255)
                            ->columns(2),
                        Forms\Components\TextInput::make('email')
                            ->label('Email')
                            ->required()
                            ->columns(2),
                        Forms\Components\Select::make('type')
                            ->label('Type')
                            ->options(UserRoles::toArray())
                            ->afterStateUpdated(function (Get $get, Set $set) {
                                if ($get('type') == UserRoles::CUSTOMER->value) {
                                    $set('groups', null);
                                }

                            })
                            ->live()
                            ->required(),
                        Forms\Components\Select::make('groups')
                            ->relationship('groups')
                            ->label('Groups')
                            ->options(Group::all()->pluck('name', 'id'))
                            ->multiple()
                            ->required()
                            ->hidden(fn(Get $get) => $get('type') == UserRoles::CUSTOMER->value),
                    ])->columns(2),
                ])->columnSpanFull(),
            ]);
Was this page helpful?