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) ?? [];
    }
}

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?
Was this page helpful?