Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TeamRed's avatar

EditAction in Livewire component not filling in form

I have this Tree Structure where I want to edit each individual element. I'm using the Filament modal to handle this and rendering it like this (this piece of code sits inside a loop):

 <div>
     {{ ($this->edit)(['post' => $item['id']]) }}

    <x-filament-actions::modals />
</div>

The wrapper Livewire component handles the edit like this:

 public function edit(): Action
    {
        return Action::make('edit')
        ->icon('heroicon-m-pencil-square')
        ->iconButton()
        ->color('gray')
        ->form([
            TextInput::make('title')
                ->maxLength(255),
            MainSubjectSelect::make('parent_id')
                ->label('Parent element')
                ->columnSpanFull()
                ->statePath('parent_id'),
        ])
        ->action(function (array $arguments) {
            $post = SubjectTree::find($arguments['post']);

            $post->update([
                'title' => request()->input('title'),
                'parent_id' => request()->input('parent_id'),
            ]);

            $this->emit('refreshItems');  // Emit an event to refresh the items list
        });
    }

The problem is that the form is not filled in. It does retrieve the correct $post, when i dd($post) after the action it shows me the correct $post. How do I handle this?

0 likes
2 replies
LaryAI's avatar
Level 58

To ensure that the form fields are pre-filled with the existing data when editing an item in a Livewire component, you need to set the initial state of the form fields with the data from the database. In your case, you need to pass the existing values of the title and parent_id to the form when the modal is opened.

Here's how you can modify your edit method to achieve this:

  1. Set Initial State for Form Fields: Use the state method to set the initial values for the form fields based on the data retrieved from the database.

  2. Pass the Data to the Form: When you call the edit action, ensure that the data is passed correctly to the form.

Here's an updated version of your edit method:

Key Points:

  • State Initialization: The state method is used to set the initial value of each form field. This ensures that when the modal is opened, the fields are pre-filled with the current values from the database.

  • Mounting the Form: The mountUsing method is used to fill the form with the existing data when the modal is opened. This is crucial for ensuring that the form fields are populated with the correct data.

  • Ensure Correct Data Passing: Make sure that the $item['id'] is correctly passed to the edit action so that the correct record is retrieved and its data is used to fill the form.

By implementing these changes, your form should be correctly pre-filled with the existing data when you open the edit modal.

jaseofspades88's avatar

Have you tried the following...

Action::make('action_name')
    ->fillForm(fn (Model $record): array => [
        'title' => $record->title
    ])
    ->form([
        TextInput::make('title')
            ->label('User')
            ->required(),
    ])
    ->action(function (array $data, Model
		//do whatever
    })

The special sauce here, is the fillForm method, where you can inject the model you need and then return a key value pair in the array, which should populate your form accordingly

Please or to participate in this conversation.