© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•2y ago•
3 replies
Jon Mason

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
wire:click
in my blade file, which calls my
loadRecent
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())
$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.

        $this->form->fill($data);
        $form = $this->getForm('form');
        $this->form($form);
        $this->form->fill($data);
        $form = $this->getForm('form');
        $this->form($form);
Jump to solution
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Dynamic form - CheckboxList issue
FilamentFFilament / ❓┊help
2y ago
Dynamic Form Component
FilamentFFilament / ❓┊help
2y ago
Dynamic Form Validations
FilamentFFilament / ❓┊help
2y ago
Dynamic Form Field
FilamentFFilament / ❓┊help
3y ago