FilamentF
Filament3y ago
ico

How to dynamically generate Field schemas in Filament v3

I have 2 sections

1st the user will pick a item from a select
After it is picked the 2nd section needs to be visible and be filled with many text inputs based on what the user has picked from the select.
But when i try to generate the schema on the spot it gives me a error Property type not supported in Livewire for property: [{}]

Code:
Section::make('AQL Report Form')
    ->schema([
        TextInput::make('limitCardNumber')
            ->label('Limit Card')
            ->disabled(true),
        TextInput::make('orderQuantity')
            ->label('Order Quantity')
            ->disabled(true),
        Select::make('stageId')
            ->options($this->stages)
            ->label('Stage')
            ->afterStateUpdated(function (Set $set, Get $get) {
                $this->updateDefects($this->stageId);
            })
            // This is needed for the stateUpdated method to work
            ->reactive(),
    ])
    ->columns(3),
Section::make('Defect List')
    ->schema(function () {
        return $this->defectListSchema;
    })
    ->columns(3)
    ->hidden(function () {
        return $this->hideDefectList;
    })
    ->reactive(),


The updateDefects() triggers a generateDefectListSchema() method for generating the 2nd section schema

generateDefectListSchema()

$schema = [];
$defectCategories = DefectCategory::get()->pluck('category', 'id')->toArray();
if (is_null($this->defectsByCategory)) {
    return $schema;
}
foreach ($this->defectsByCategory as $category => $defects) {
    $sectionSchema = [];
    foreach ($defects as $defect) {
        $sectionSchema = array_merge($sectionSchema, [
            TextInput::make('defectName')->label($defect['name'])->disabled(true),
        ]);
    }
    $schema = array_merge($schema, [
        Section::make($defectCategories[$category])
            ->schema($sectionSchema),
    ]);
}
return $schema;

The $schema is filled correctly i can see the result when i dd($schema)
Solution
changed it protected
Was this page helpful?