I am currently working on a web-based form application using Laravel 10 and Filament PHP. I have a form table with columns containing data as follows:
[
{
"value": "John Doe",
"desc": "Full Name",
"type": "string",
"valid": false
},
{
"value": "Elm Street No. 13",
"desc": "Address",
"type": "text",
"valid": false
},
{
"value": "0142365789",
"desc": "Phone Number",
"type": "string",
"valid": false
}
]
Now, I want to display this data using the Form Repeater provided by Filament. Below is a snippet of my code:
Step::make('Form')
->description('Form submission')
->schema([
FormsComponentsRepeater::make('form')->schema(function (Get $get): array {
switch ($get('type')) {
case 'string':
$formField = TextInput::make('value');
break;
case 'text':
$formField = Textarea::make('value');
break;
case 'image':
$formField = SpatieMediaLibraryFileUpload::make('value')->getUploadedFiles();
break;
case 'file':
$formField = FileUpload::make('value')->downloadable();
break;
case 'date':
$formField = DatePicker::make('value')->displayFormat(function (): string {
return 'd/m/Y';
});
break;
default:
$formField = TextInput::make('value');
}
return [
$formField,
Textarea::make('note'),
Toggle::make('valid'),
];
})->addable(false)
->deletable(false)
->reorderable(false)
->reorderableWithDragAndDrop(false)
->columnSpanFull(),
])
When I tried running the code I didn’t encounter any errors at all and the data appeared by what was stored in the table until finally I realized that the data did not appear according to the type of each data. For example, if the data type is a string, a TextInput
should be displayed, and if it’s a file, a FileInput
should be displayed. The problem is, that I’m not sure how to correctly access the data type from the form column before displaying it. When I tried adding dd($get('type))
, it returns null
.
Can anyone help me solve this issue? Any advice or answers would be greatly appreciated. Thank you.