It seems like the issue you're encountering is related to the type of data that the KeyValue component is expecting. The error message indicates that the KeyValue component's closure is expecting a string, but it's receiving an array instead.
The KeyValue component in Filament forms is designed to handle an array of key-value pairs, where both the key and the value are strings. However, when you're editing an existing model, the cast to array in your model might be providing an actual array to the component, which it doesn't expect for individual key or value fields.
To resolve this issue, you can try to ensure that the KeyValue component receives the data in the expected format. You might need to transform the array data into a string representation before it's passed to the KeyValue component, and then convert it back to an array when saving.
Here's how you can modify your form to handle the transformation:
public static function form(Form $form): Form
{
return $form
->schema([
// ... other form components ...
Forms\Components\KeyValue::make('score_description')
->keyLabel('score')
->valueLabel('description')
->dehydrateStateUsing(function ($state) {
return json_encode($state);
})
->hydrateStateUsing(function ($state) {
return json_decode($state, true) ?? [];
}),
Forms\Components\KeyValue::make('score_description_fr')
->keyLabel('score')
->valueLabel('description')
->dehydrateStateUsing(function ($state) {
return json_encode($state);
})
->hydrateStateUsing(function ($state) {
return json_decode($state, true) ?? [];
}),
// ... other form components ...
])->columns(1);
}
In this solution, we're using the dehydrateStateUsing and hydrateStateUsing methods to transform the state when it's being saved and loaded. The dehydrateStateUsing method is used to convert the array into a JSON string before it's saved, and the hydrateStateUsing method is used to convert the JSON string back into an array when the form is being populated for editing.
Make sure that your model's $casts attribute is still in place to handle the JSON to array conversion when interacting with the database:
protected $casts = [
'score_description' => 'array',
'score_description_fr' => 'array',
];
This should resolve the type mismatch issue you're experiencing with the KeyValue component when editing your model.