filament icon indicating copy to clipboard operation
filament copied to clipboard

Bug: default() values are not applied when editing a record if the input is conditionally visible

Open akbarali1 opened this issue 8 months ago • 1 comments

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'),
]);

} `

Image

Image

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


akbarali1 avatar Apr 23 '25 07:04 akbarali1

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.

akbarali1 avatar Apr 23 '25 08:04 akbarali1

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

danharrin avatar Jun 22 '25 22:06 danharrin