Filament Form: Dynamic additional_fields values not being saved properly

Hi everyone, I'm having an issue with dynamic fields in Filament where values entered in the form are not being captured properly during the save process. My setup: - I'm using a JSON column named additional_fields in my database - In ProductResource, I dynamically generate form fields based on product groups - These fields appear correctly in the form and I can enter values - When I submit the form, all values in these dynamic fields become null or empty strings Here's my current implementation:
// In ProductResource
Section::make('Ek Bilgi Alanları')
->schema(fn(Get $get): array => self::getAdditionalFields($get('product_group_ids')))
->columns(2)
->statePath('additional_fields')
->dehydrated(true)

// getAdditionalFields method
protected static function getAdditionalFields(?array $productGroupIds): array
{
// ... get fields

return $additionalFields->map(function ($field) {
$fieldKey = Str::snake($field->value ?: $field->field_name);

$component = match ($field->field_type) {
'free-text' => TextInput::make("additional_fields.{$fieldKey}")
->label($field->value ?: $field->field_name)
->default(''),

};

return $component;
})->toArray();
Product Model
public function setAdditionalFieldsAttribute($value)
{
$this->attributes['additional_fields'] = is_array($value) ? json_encode($value) : $value;
}
public function getAdditionalFieldsAttribute($value)
{
return json_decode($value, true) ?? [];
}
}
// In ProductResource
Section::make('Ek Bilgi Alanları')
->schema(fn(Get $get): array => self::getAdditionalFields($get('product_group_ids')))
->columns(2)
->statePath('additional_fields')
->dehydrated(true)

// getAdditionalFields method
protected static function getAdditionalFields(?array $productGroupIds): array
{
// ... get fields

return $additionalFields->map(function ($field) {
$fieldKey = Str::snake($field->value ?: $field->field_name);

$component = match ($field->field_type) {
'free-text' => TextInput::make("additional_fields.{$fieldKey}")
->label($field->value ?: $field->field_name)
->default(''),

};

return $component;
})->toArray();
Product Model
public function setAdditionalFieldsAttribute($value)
{
$this->attributes['additional_fields'] = is_array($value) ? json_encode($value) : $value;
}
public function getAdditionalFieldsAttribute($value)
{
return json_decode($value, true) ?? [];
}
}
The form logs show: [2025-03-06] local.INFO: Düzeltilmiş form verileri: {"name":"product name","product_group_ids":["1","2"],"additional_fields":{"cinsiyet":"","etiketler":"","fiyat":"","tema":""}} Despite entering values in the form, they always come through as null. Has anyone encountered this issue before? Any suggestions on how to properly capture values from dynamic fields?
2 Replies
Mehmet K.
Mehmet K.OP5w ago
I should also mention that when editing, the data is saved and comes in. This problem only occurs during the initial creation. https://codeshare.io/r4JJNq Of course, I look forward to your help.
John
John5w ago
That getAdditionalFieldsAttribute and setAdditionalFieldsAttribute is old Laravel syntax. On which version are you? Is it still supported? You can simply add a cast to your model:
protected $casts = [
'additional_fields' => 'array', // or 'json' which is an alias
];
protected $casts = [
'additional_fields' => 'array', // or 'json' which is an alias
];
Also, you don't need to ->dehydrate(true) and I don't know if that interferes with default behaviour. But maybe that's from testing stuff.

Did you find this page helpful?