F
Filament13mo ago
Naren

Hidden attribute not working as expected for Sections in Filament Form

Hello, I'm currently using the Filament package for a project and I've encountered an issue. I am trying to hide a Forms\Components\Section based on the value of a Forms\Components\Select input field using the hidden method. It appears that the hidden method works correctly when used with Forms\Components\DatePicker, but it does not behave as expected when applied to Forms\Components\Section. Is this a known issue, or am I perhaps using it incorrectly? Here is a snippet of the code I'm working with:
Forms\Components\Section::make('Contract')
->relationship('contract')
->hidden(function (Closure $get) {
return $get('employee_type_id') !== 3;
})
->schema([
Forms\Components\DatePicker::make('start_date'),
Forms\Components\DatePicker::make('end_date'),
Forms\Components\TextArea::make('terms')->columnSpan('full'),
])
->columns(2),
Forms\Components\Section::make('Contract')
->relationship('contract')
->hidden(function (Closure $get) {
return $get('employee_type_id') !== 3;
})
->schema([
Forms\Components\DatePicker::make('start_date'),
Forms\Components\DatePicker::make('end_date'),
Forms\Components\TextArea::make('terms')->columnSpan('full'),
])
->columns(2),
In this case, the Contract section should be hidden unless the employee_type_id is 3, but it seems to always be hidden, regardless of the employee_type_id value. Any assistance would be greatly appreciated. Thank you.
11 Replies
Kenneth Sese
Kenneth Sese13mo ago
What does your select field look like? Also, try != 3. I believe values returned as strings not ints.
Naren
Naren13mo ago
Forms\Components\Section::make('Employment')
->relationship('employment')
->reactive()
->schema([

Forms\Components\Select::make('employee_type_id')
->reactive()
->relationship('employeeType', 'name')


])
->columns(2),

Forms\Components\Section::make('Contract')
->relationship('contract')
->reactive()
->hidden(function (Closure $get) {
return $get('employee_type_id') != 3;
})
->schema([
Forms\Components\DatePicker::make('start_date'),
Forms\Components\DatePicker::make('end_date'),
Forms\Components\TextArea::make('terms')->columnSpan('full'),
])
->columns(2),
Forms\Components\Section::make('Employment')
->relationship('employment')
->reactive()
->schema([

Forms\Components\Select::make('employee_type_id')
->reactive()
->relationship('employeeType', 'name')


])
->columns(2),

Forms\Components\Section::make('Contract')
->relationship('contract')
->reactive()
->hidden(function (Closure $get) {
return $get('employee_type_id') != 3;
})
->schema([
Forms\Components\DatePicker::make('start_date'),
Forms\Components\DatePicker::make('end_date'),
Forms\Components\TextArea::make('terms')->columnSpan('full'),
])
->columns(2),
I tried != 3 it's not working
Lara Zeus
Lara Zeus13mo ago
I tested your code and it's works fine I switched the relation with values
Select::make('employee_type_id')
->options([
'1'=>'1',
'2'=>'2',
'3'=>'3',
])
->reactive()
Select::make('employee_type_id')
->options([
'1'=>'1',
'2'=>'2',
'3'=>'3',
])
->reactive()
try
->hidden(function (\Closure $get) {
dd($get , $get('employee_type_id'));
return $get('employee_type_id') != 3;
})
->hidden(function (\Closure $get) {
dd($get , $get('employee_type_id'));
return $get('employee_type_id') != 3;
})
to find out what is the actual value
Kenneth Sese
Kenneth Sese13mo ago
I believe the problem is your select is inside another relationship. Try return $get('employment.employee_type_id') != 3; Also, might want to use ->visible() instead so you're not dealing with a double negative
Naren
Naren13mo ago
Yes this one working fine
Forms\Components\Section::make('Contract')
->relationship('contract')
->hidden(function (\Closure $get) {
return $get('employment.employee_type_id') != 3;
})
->reactive()
->schema([
Forms\Components\DatePicker::make('start_date'),
Forms\Components\DatePicker::make('end_date'),
Forms\Components\TextArea::make('terms')->columnSpan('full'),
])
->columns(2),
Forms\Components\Section::make('Contract')
->relationship('contract')
->hidden(function (\Closure $get) {
return $get('employment.employee_type_id') != 3;
})
->reactive()
->schema([
Forms\Components\DatePicker::make('start_date'),
Forms\Components\DatePicker::make('end_date'),
Forms\Components\TextArea::make('terms')->columnSpan('full'),
])
->columns(2),
Thank you for your support
Ander
Ander11mo ago
And if it was the other way around? I am inside the relation and I want the value of a field outside of that relation
Kenneth Sese
Kenneth Sese11mo ago
I think you’d just use $get with the field name. Is that not working? When you’re inside a repeater you have to use the ../ syntax to get back to the parent, but in relationships I believe it’s just the normal field name.
Ander
Ander11mo ago
I tried like this but it doesn't work, I thought there was a reverse way
Fieldset::make()
->schema([
Select::make('employee_type_id')->label('tipo de empleado')->options(EmployeeType::all()->pluck('name', 'id'))->required()->columnSpan('full')
->afterStateUpdated(function (Closure $set, $state){ $set('employeeJob.test', $state);})->reactive(),
Group::make()
->relationship('employeeJob')
->schema([
Grid::make(['sm' => 1, 'md' => 2, 'xl' => 2])
->schema([
Select::make('type')->label(__('employees.type_employee'))
->options([
'salary' => __('employees.salary'),
'salary_overtime' => __('employees.salary_overtime'),
'hourly' => __('employees.hourly'),
])->reactive()->required(),

TextInput::make('test'),
TextInput::make('payment_amount')->label(__('employees.amount'))
->mask(fn (TextInput\Mask $mask) => $mask->money(prefix: 'GTQ ', thousandsSeparator: ',', decimalPlaces: 2, isSigned: false))
->rules([
function (Closure $get)
{
\Log::alert($get('employee_type_id'));
}
])
->numeric()->required()

]),
Textarea::make('change_reason')->label(__('employees.change_reason'))->visibleOn('edit')->columnSpan('full'),
])->columnSpan('full'),
])
Fieldset::make()
->schema([
Select::make('employee_type_id')->label('tipo de empleado')->options(EmployeeType::all()->pluck('name', 'id'))->required()->columnSpan('full')
->afterStateUpdated(function (Closure $set, $state){ $set('employeeJob.test', $state);})->reactive(),
Group::make()
->relationship('employeeJob')
->schema([
Grid::make(['sm' => 1, 'md' => 2, 'xl' => 2])
->schema([
Select::make('type')->label(__('employees.type_employee'))
->options([
'salary' => __('employees.salary'),
'salary_overtime' => __('employees.salary_overtime'),
'hourly' => __('employees.hourly'),
])->reactive()->required(),

TextInput::make('test'),
TextInput::make('payment_amount')->label(__('employees.amount'))
->mask(fn (TextInput\Mask $mask) => $mask->money(prefix: 'GTQ ', thousandsSeparator: ',', decimalPlaces: 2, isSigned: false))
->rules([
function (Closure $get)
{
\Log::alert($get('employee_type_id'));
}
])
->numeric()->required()

]),
Textarea::make('change_reason')->label(__('employees.change_reason'))->visibleOn('edit')->columnSpan('full'),
])->columnSpan('full'),
])
Kenneth Sese
Kenneth Sese11mo ago
Yeah the reverse way ../ is for repeaters. I’ll have to look at this later on my computer. Discord is horrible at formatting code on mobile
Ander
Ander11mo ago
thanks, this worked!!! don't worry, thanks for helping me
Kenneth Sese
Kenneth Sese11mo ago
Oh. Ok! Cool!