Bug: default() values are not applied when editing a record if the input is conditionally visible
Package
filament/forms
Package Version
v3.3
Laravel Version
v12.0
Livewire Version
No response
PHP Version
PHP 8.4.6
Problem description
When building a dynamic form in Filament (v3.x), we want to show different inputs based on the value of a Select::make('type'). The fields are correctly shown/hidden using visible(), but on the edit page, the existing value is not populated in the input, even though default(fn ($record) => ...) is used.
This makes it impossible to see or edit the previously saved value when editing a record.
Expected behavior
The appropriate input should be visible based on the selected type.
Its value should be populated from the $record->value.
Steps to reproduce
`public static function form(Form $form): Form { return $form->schema([ Select::make('type') ->label('Type') ->required() ->reactive() ->disabledOn('edit') ->default('text') ->options([ 'text' => 'Text', 'number' => 'Number', 'date' => 'Date', 'file' => 'File', ]),
TextInput::make('value')
->label('Text Value')
->visible(fn (Get $get) => $get('type') === 'text')
->default(fn ($record) => $record?->type === 'text' ? $record->value : null)
->dehydrated(fn (Get $get) => $get('type') === 'text'),
TextInput::make('value')
->label('Number Value')
->numeric()
->visible(fn (Get $get) => $get('type') === 'number')
->default(fn ($record) => $record?->type === 'number' ? $record->value : null)
->dehydrated(fn (Get $get) => $get('type') === 'number'),
DatePicker::make('value')
->label('Date Value')
->visible(fn (Get $get) => $get('type') === 'date')
->default(fn ($record) => $record?->type === 'date' ? $record->value : null)
->dehydrated(fn (Get $get) => $get('type') === 'date'),
]);
} `
CODE: https://github.com/akbarali1/bug_reports/blob/main/filament/form/SettingResource.php COMPOSER FILE: https://github.com/akbarali1/bug_reports/blob/main/filament/form/composer.json
Reproduction repository (issue will be closed if this is not valid)
https://github.com/akbarali1/bug_reports/blob/main/filament/form/SettingResource.php
Relevant log output
But if I use only one input (without conditional visibility), then its value is displayed correctly. In other words, if I remove the number, DatePicker, and FileUpload inputs, the value is loaded and shown as expected.
Hi! Default values are only applied on Create and not Edit pages, so as we dont overwrite existing data. See https://filamentphp.com/docs/3.x/forms/fields/getting-started#setting-a-default-value
You could use $set() to set the value of the input in afterStateUpdated() when you choose to show it: https://filamentphp.com/docs/3.x/forms/fields/getting-started#setting-a-default-value, https://filamentphp.com/docs/3.x/forms/advanced#field-updates