Form: Nested JSON values are stored as string instead of integers

I have a form like:
public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('config.file_expire_days')
->integer()
->default(30)
->required(),
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('config.file_expire_days')
->integer()
->default(30)
->required(),
]);
}
And my model even casts the json values:
protected $casts = [
'config' => 'array',
'config->notify_project_expire_days' => 'array',
'config->project_expire_days' => 'integer',
'config->file_expire_days' => 'integer',
];
protected $casts = [
'config' => 'array',
'config->notify_project_expire_days' => 'array',
'config->project_expire_days' => 'integer',
'config->file_expire_days' => 'integer',
];
But if I save my form the model is created in the databse with string values in the JSON column (see the file_expire_days): {"base_path": "test", "file_expire_days": "15", "auto_delete_files": true, "project_expire_days": 30, "notify_project_expire_days": [14, "7", "1"]} If I leave the default value it is saved correctly as an integer. How can I tell filament that the values should be stored according to the model cast?
2 Replies
LeandroFerreira
LeandroFerreira9mo ago
TextInput::make('config.file_expire_days')
->dehydrateStateUsing(fn(?string $state) => (int) $state)
TextInput::make('config.file_expire_days')
->dehydrateStateUsing(fn(?string $state) => (int) $state)
?
Nuekrato
NuekratoOP9mo ago
Thanks @Leandro Ferreira this works. Is it possible to build something like this directly into Filament? Ideally by respecting the model $casts-property?

Did you find this page helpful?