dynamic form schema timing issue

I've got a form that should only be loaded when a user clicks on an item in a list (not on page load). To handle this I have wire:click in my blade file, which calls my loadRecent method. This method loads the necessary data that drives which fields will be on my form. The form data is unknown until the user clicks an item, so having a form with a standard set of hidden fields isn't an option. My issue is that $form->schema($this->getSchema()) seems to be getting called early in the lifecycle before when my data is populated as is evidenced by the logs referenced in the code below.
[2024-08-19 09:50:21] local.DEBUG: getSchema 09:50:21 [[]]
[2024-08-19 09:50:22] local.DEBUG: loadRecent: 09:50:22 [[{"id":773,"
[2024-08-19 09:50:21] local.DEBUG: getSchema 09:50:21 [[]]
[2024-08-19 09:50:22] local.DEBUG: loadRecent: 09:50:22 [[{"id":773,"
//called from blade view with wire:click
public function loadRecent($id)
{
$this->salesRec = SalesReconciliation::find($id);
$items = $this->salesRec->items;

//do other stuff.

$this->items = $items->toArray();

$this->form->fill();
//not empty. seems to run AFTER $form->schema
Log::debug('loadRecent', [$this->items]);
}

public function form(Form $form): Form
{
return $form->schema($this->getSchema())
->statePath('data');
//->model(SalesReconciliationItem::class);
}

public function getSchema(): array
{
$array = [];

//empty array in logs.
Log::debug('getSchema', [$this->items]);

foreach ($this->items as $item) {
$array[] = MathInput::make($item['account_ref_id'])
->label($item['account_name'])
->live()
->afterStateUpdated(function ($state, $old) use ($item) {
//do stuff
});
}

return $array;
}
//called from blade view with wire:click
public function loadRecent($id)
{
$this->salesRec = SalesReconciliation::find($id);
$items = $this->salesRec->items;

//do other stuff.

$this->items = $items->toArray();

$this->form->fill();
//not empty. seems to run AFTER $form->schema
Log::debug('loadRecent', [$this->items]);
}

public function form(Form $form): Form
{
return $form->schema($this->getSchema())
->statePath('data');
//->model(SalesReconciliationItem::class);
}

public function getSchema(): array
{
$array = [];

//empty array in logs.
Log::debug('getSchema', [$this->items]);

foreach ($this->items as $item) {
$array[] = MathInput::make($item['account_ref_id'])
->label($item['account_name'])
->live()
->afterStateUpdated(function ($state, $old) use ($item) {
//do stuff
});
}

return $array;
}
Solution:
ok, I was able to do this in my loadRecent method, which works. ```php $this->form->fill($data); $form = $this->getForm('form');...
Jump to solution
1 Reply
Solution
Jon Mason
Jon Mason4mo ago
ok, I was able to do this in my loadRecent method, which works.
$this->form->fill($data);
$form = $this->getForm('form');
$this->form($form);
$this->form->fill($data);
$form = $this->getForm('form');
$this->form($form);
Want results from more Discord servers?
Add your server