F
Filament16mo 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(),
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;
$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
Jump to solution
9 Replies
ico
icoOP16mo ago
dd($schema) :
No description
ico
icoOP16mo ago
so it is filled but when i remove the dd() i get
No description
ico
icoOP16mo ago
These are my properties
public $stageId;
public $defectId;

public $limitCardNumber;
public $orderQuantity;

public $sample;
public $inspectionLevel;

public $acceptedDefects;
public $numberOfDefects;

public array $stages = [];
public array $defectListSchema = [];

private ?Collection $defectsByCategory = null;

public $hideDefectList = true;
public $stageId;
public $defectId;

public $limitCardNumber;
public $orderQuantity;

public $sample;
public $inspectionLevel;

public $acceptedDefects;
public $numberOfDefects;

public array $stages = [];
public array $defectListSchema = [];

private ?Collection $defectsByCategory = null;

public $hideDefectList = true;
wyChoong
wyChoong16mo ago
Share your mount function
ico
icoOP16mo ago
public function mount(): void
{
$this->stages = Stage::get()->pluck('name', 'id')->toArray();

$this->form->fill([
'limitCardNumber' => 95293,
'orderQuantity' => 504000,
]);
}
public function mount(): void
{
$this->stages = Stage::get()->pluck('name', 'id')->toArray();

$this->form->fill([
'limitCardNumber' => 95293,
'orderQuantity' => 504000,
]);
}
wyChoong
wyChoong16mo ago
Probably check again where you assign values to the properties Share the whole code in a gist if still not solved
ico
icoOP16mo ago
The problem seems to be in the $defectListSchema property after it is filled
Solution
ico
ico16mo ago
changed it protected
ico
icoOP16mo ago
it worked

Did you find this page helpful?