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

lara262588's avatar

Using a Livewire form inside a Filament Resource (simple)

Hello everyone, I have an issue while trying to use a custom Livewire component inside a modal for a resource (I'm using the --simple version with ManageMyResource).

I have attached the code related to the Livewire component and the Filament resource. I would prefer not to use a full-page Livewire component but just be able to render the custom form inside the modal.

ManageResource:

<?php

namespace App\Filament\Resources\OpenFundsPatternResource\Pages;

use App\Filament\Resources\OpenFundsPatternResource;
use Filament\Actions;
use Filament\Resources\Pages\ManageRecords;

class ManageOpenFundsPatterns extends ManageRecords
{
    protected static string $resource = OpenFundsPatternResource::class;

    protected function getHeaderActions(): array
    {
        return [
            //Actions\CreateAction::make()

            //Not Working!! :(
            Actions\CreateAction::make()->modalContent(view('livewire.create-open-funds-d')),
        ];
    }
}

Resource:

<?php

namespace App\Filament\Resources;

use App\Filament\Resources\OpenFundsPatternResource\Pages;
use App\Models\OpenfundsD;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Carbon\Carbon;


class OpenFundsPatternResource extends Resource
{
   protected static ?string $model = OpenfundsD::class;

   protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

   protected static ?string $navigationGroup = 'Open Funds';

   protected static ?string $navigationLabel = 'Open Funds Patterns';



   public static function form(Form $form): Form
   {   
       
        return $form
           ->schema([
               /*Forms\Components\TextInput::make('ckey')
                   ->label('Header Originale')
                   ->required(),
               Forms\Components\TextInput::make('cvalue')
                   ->label('Header Finale')
                   ->required(),
               Select::make('open_funds_lunivid')
                   ->relationship(name: 'OpenfundsT', titleAttribute: 'cfilename') */

                   //CreateAction::make()->modalContent(view('livewire.create-open-funds-d'))
           ]);
   }

   public static function table(Table $table): Table
   {
       return $table
           ->columns([
               TextColumn::make('lunivid')
                   ->label('id')
                   ->toggleable()
                   ->sortable(),
               TextColumn::make('ckey')
                   ->label('Header Originale')
                   ->toggleable()
                   ->searchable(),
               TextColumn::make('cvalue')
                   ->label('Header Finale')
                   ->toggleable()
                   ->searchable(),
               TextColumn::make('tuts')
                   ->toggleable()
                   ->sortable()
                   ->label('Data')
                   ->dateTime("Y-m-d H:i:s", 'Europe/Rome'),
           ])
           ->filters([
               Filter::make('tuts')
                   ->form([
                       DatePicker::make('creato_da'),

                       DatePicker::make('creato_al'),

                   ])
                   ->query(function (Builder $query, array $data): Builder {
                       //dd($data);
                       return $query
                           ->when(
                               $data['creato_da'] ?? null,
                               fn (Builder $query, $date): Builder => $query->whereDate('tuts', '>=', $date),
                           )
                           ->when(
                               $data['creato_al'] ?? null,
                               fn (Builder $query, $date): Builder => $query->whereDate('tuts', '<=', $date),
                           );
                   })

                   ->indicateUsing(function (array $data): array {
                       $indicators = [];
                       if ($data['creato_da'] ?? null) {
                           $indicators['creato_da'] = 'Ordina da ' . Carbon::parse($data['creato_da'])->toFormattedDateString();
                       }
                       if ($data['creato_al'] ?? null) {
                           $indicators['creato_al'] = 'Order until ' . Carbon::parse($data['creato_al'])->toFormattedDateString();
                       }

                       return $indicators;
                   }),
           ])
           ->actions([
               Tables\Actions\EditAction::make()
                   ->label('')
                   ->size('lg'),
               Tables\Actions\DeleteAction::make()
                   ->label('')
                   ->size('lg'),
           ])
           ->bulkActions([
               Tables\Actions\BulkActionGroup::make([
                   Tables\Actions\DeleteBulkAction::make(),
               ]),
           ])
           ->emptyStateActions([
               Tables\Actions\CreateAction::make(),
           ]);
   }

   public static function getPages(): array
   {
       return [
           'index' => Pages\ManageOpenFundsPatterns::route('/'),
       ];
   }
}

Livewire Custom Component (actually a form, i need to do a custom drag and drop with js)

<?php

namespace App\Livewire;

use Livewire\Component;
use App\Models\Post;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\MarkdownEditor;
use Filament\Forms\Components\Select;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Illuminate\Contracts\View\View;


class CreateOpenFundsD extends Component implements HasForms
{
    use InteractsWithForms;

    public ?array $data = [];

    public function mount(): void
    {
        $this->form->fill();
    }

    public function form(Form $form): Form
    {
        return $form
            ->schema([
                TextInput::make('ckey')
                    ->label('Header Originale')
                    ->required(),
                TextInput::make('cvalue')
                    ->label('Header Finale')
                    ->required(),
                Select::make('open_funds_lunivid')
                    ->relationship(name: 'OpenfundsT', titleAttribute: 'cfilename')
            ])
            ->statePath('data');
    }

    public function create(): void
    {
        dd($this->form->getState());
    }

    public function render()
    {
        return view('livewire.create-open-funds-d');
    }
}

Livewire View:

<div>
    <form wire:submit="create">
        {{ $this->form }}
        
        <button type="submit">
            Submit
        </button>
    </form>
    
    <x-filament-actions::modals />
</div>

This way im getting this error: Too few arguments to function Filament\Resources\Pages\ListRecords::form(), 0 passed in /home/alby/pautoimporta/vendor/filament/actions/src/Concerns/InteractsWithActions.php on line 308 and exactly 1 expected

Can anyone help me with this problem? Thank you very much

0 likes
6 replies
LaryAI's avatar
Level 58

It seems like you're trying to use a Livewire component as a modal content for a Filament resource action. To achieve this, you should use the modal() method on the action to define the Livewire component that should be rendered inside the modal.

Here's how you can modify your ManageOpenFundsPatterns class to use the Livewire component inside a modal:

namespace App\Filament\Resources\OpenFundsPatternResource\Pages;

use App\Filament\Resources\OpenFundsPatternResource;
use Filament\Pages\Actions;
use Filament\Resources\Pages\ManageRecords;

class ManageOpenFundsPatterns extends ManageRecords
{
    protected static string $resource = OpenFundsPatternResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Actions\CreateAction::make()
                ->modal(function (Actions\Modal $modal) {
                    return $modal->schema([
                        // Here you can define the fields if you want to use Filament forms
                        // Or you can simply render a Livewire component like this:
                        $this->livewire(\App\Livewire\CreateOpenFundsD::class),
                    ]);
                }),
        ];
    }
}

In the above code, we're using the modal() method to define the modal content. Inside the closure, we're calling $this->livewire() and passing the Livewire component class name as an argument. This will render the Livewire component inside the modal.

Make sure that your Livewire component is properly registered and that the view file livewire.create-open-funds-d exists and is correctly set up to render the Livewire component.

Also, ensure that your Livewire component's render() method returns the correct view:

public function render()
{
    return view('livewire.create-open-funds-d', [
        'form' => $this->form($this->form(Form::make())) // Pass the form to the view
    ]);
}

And in your Livewire view, you should be able to render the form like this:

<div>
    <form wire:submit.prevent="create">
        {{ $form }}
        
        <button type="submit">
            Submit
        </button>
    </form>
</div>

Make sure that your Livewire component's create method is correctly handling the form submission. The dd($this->form->getState()); line will dump the form state, but you'll want to replace that with your actual logic for handling the form submission.

By following these steps, you should be able to render a Livewire component inside a modal for a Filament resource action.

chrishanson's avatar

The AI does not offer a valid solution, doesn't work for me.

chrishanson's avatar

@glabdugrib did you find a solution to this?

I have it half working, now frontend just complains the component ID can't be found when clicking submit.

LucasCis92's avatar

@chrishanson Hi! How did you manage to make it work? I'm having the exact same issue. It says that must be of type Form, and Infolist is given. I'm using the HasForms and the InteractsWithForms

flashman's avatar

Hi, I also have same problem. I am using Filament v4. I have Action that renders Livewire component. That Livewire components uses HasForms and InteracstWithForms trait. I have defined schema with simple field, a configured view to submit to my create metod in component. But what I want is to save data, render livewire component, but my action modal closes, and create method is never triggered.

Please or to participate in this conversation.