Issue with Fieldset Visibility on Form Edit first render

Context: I'm working with Filament forms and have a fieldset (Student Information) that should only be visible when the role field is set to 4. The role is set via a Select field where I assign a role ID. The Problem: - When editing a user with an existing role ID (e.g., 4), the Student Information fieldset doesn't appear on first render until I change the role and then set it back to 4. - I expect the fieldset to be visible as soon as the form is rendered if the user’s role id is already 4, but it remains hidden until the role is selected again. Code Example:
<?php

namespace App\Filament\Resources\UserResource\Forms;

use App\Models\Student;
use App\Models\User;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Illuminate\Database\Eloquent\Builder;

class UserForm
{
public function form(Form $form): Form
{
return $form->schema([
Forms\Components\Fieldset::make('Account Information')->schema([
Forms\Components\Select::make('role')
->label('Role')
->relationship('roles', 'name', fn(Builder $query) => $query->where('name', '!=', 'Admin'))
->preload()
->live()
->searchable()
->required(),
]),
Forms\Components\Fieldset::make('Student Information')->schema([
Forms\Components\TextInput::make('nim')->required(),
Forms\Components\Select::make('gender')->required(),
])->visible(fn(Get $get) => $get('role') === '4')
]);
}
}
<?php

namespace App\Filament\Resources\UserResource\Forms;

use App\Models\Student;
use App\Models\User;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Illuminate\Database\Eloquent\Builder;

class UserForm
{
public function form(Form $form): Form
{
return $form->schema([
Forms\Components\Fieldset::make('Account Information')->schema([
Forms\Components\Select::make('role')
->label('Role')
->relationship('roles', 'name', fn(Builder $query) => $query->where('name', '!=', 'Admin'))
->preload()
->live()
->searchable()
->required(),
]),
Forms\Components\Fieldset::make('Student Information')->schema([
Forms\Components\TextInput::make('nim')->required(),
Forms\Components\Select::make('gender')->required(),
])->visible(fn(Get $get) => $get('role') === '4')
]);
}
}
The Issue Details: - Expected Behavior: The Student Information fieldset should be visible as soon as the form loads if the role field is set to 4. - Actual Behavior: The fieldset is hidden on first render, and only becomes visible after I change the role and select 4 again.
4 Replies
ashtrath
ashtrathOP3mo ago
anyone?
Dennis Koch
Dennis Koch3mo ago
The column probably should be role_id instead of role
toeknee
toeknee3mo ago
However, you will get issues by excluded admin if you try to edit an admin fyi.
Bruno Pereira
Bruno Pereira3mo ago
And I would also check the visibility based on the record field instead of the state of the select, because if you're already editing a user then he probably has a role. If you by chance need to change the role put an or condition checking the field state.
->visible(funtion(Get $get){
if ($this->record->role_id === '4' || $get('role') === '4')
})
->visible(funtion(Get $get){
if ($this->record->role_id === '4' || $get('role') === '4')
})
change $this->record->role_id to real case of your project

Did you find this page helpful?