Conditional Field Population Based on Active Tab in Filament v3 User Resource.

I am working on a Filament v3 app and have a UserResource with three tabs: Super Admin, Group Admin, and Clinic Users. Each tab contains its own set of fields. I want to ensure that when I create a user from the Clinic Users tab, only the fields from that tab are populated, and the other fields are set to null. The same should apply to the other tabs as well. How can I achieve this in Filament v3?
2 Replies
bernhard
bernhard6d ago
If yes, then you could use the current request activeTab value and change the target action/url of the CreateAction button based on it. Something like this:
protected function getActions(): array
{
return [
Actions\CreateAction::make()
->action(null)
->url(fn ($livewire) => YourResource::getUrl('create', [
'newType' => request("activeTab")
]))
];
}
protected function getActions(): array
{
return [
Actions\CreateAction::make()
->action(null)
->url(fn ($livewire) => YourResource::getUrl('create', [
'newType' => request("activeTab")
]))
];
}
Now when clicking on the new button, it will get you to the Create Page, but with the name of your Tab added to the url. Something like /users/create?newType=clinicUsers And now you can work with this param in your form. For example.
Hidden::make('user_type')
->default(fn() => request("newType") ?? "admin"),
TextInput::make("clinic_name")
->visible(fn($get) => $get("user_type") === "clinicUsers"),
TextInput::make("admin_name")
->visible(fn($get) => $get("user_type") === "admin"),
Hidden::make('user_type')
->default(fn() => request("newType") ?? "admin"),
TextInput::make("clinic_name")
->visible(fn($get) => $get("user_type") === "clinicUsers"),
TextInput::make("admin_name")
->visible(fn($get) => $get("user_type") === "admin"),

Did you find this page helpful?