F
Filament14mo ago
urbycoz

Why doesn't my Filament form populate on load?

Sorry for the beginners question. I thought $this->form->fill would cause my TextInput field to get prepopulated. But it doesn't. Here's my Livewire Form php class. <?php declare(strict_types=1); namespace App\Livewire; use App\Models\Legacy\Individual; use Livewire\Component; use Filament\Forms\Components\TextInput; use Filament\Forms; use Filament\Forms\Contracts\HasForms; use Filament\Forms\Concerns\InteractsWithForms; use Filament\Forms\Components\Section; use Symfony\Component\HttpFoundation\Request; class InvestorIndividualForm extends Component implements HasForms { use InteractsWithForms; public function mount(Request $request) { $this->form->fill(['firstname' => 'Bob']); } protected function getFormSchema(): array { return [TextInput::make('firstname')]; } }
Solution:
It's set when you fill() the form. Your component would look like this ```php <?php ...
Jump to solution
6 Replies
toeknee
toeknee14mo ago
That looks good Any error in your browser console?
toeknee
toeknee14mo ago
Try adjusting your mount: public function mount(Post $post): void { $this->form->fill($post->toArray()); } As an example as per: https://filamentphp.com/docs/3.x/forms/adding-a-form-to-a-livewire-component
ChesterS
ChesterS14mo ago
you need a $firstname public property. or you can use the v3 way of declaring forms so either add public string $firstname = '' to your component or
public array $data = [];

public function form(Form $form): Form
{
return $form
->schema($this->getFormSchema())
->statePath('data');
}
public array $data = [];

public function form(Form $form): Form
{
return $form
->schema($this->getFormSchema())
->statePath('data');
}
@urbycoz
urbycoz
urbycozOP14mo ago
I'd like to do it the v3 way ideally. So where does the $data property get set?
Solution
ChesterS
ChesterS14mo ago
It's set when you fill() the form. Your component would look like this
<?php

class InvestorIndividualForm extends Component implements HasForms
{
use InteractsWithForms;

public array $data = [];

public function mount(Request $request)
{
$this->form->fill(['firstname' => 'Bob']);
}

public function form(Form $form): Form
{
return $form
->schema([TextInput::make('firstname')])
->statePath('data'); // <- This is where you tell it to store the form information in the 'data' array
}
}
<?php

class InvestorIndividualForm extends Component implements HasForms
{
use InteractsWithForms;

public array $data = [];

public function mount(Request $request)
{
$this->form->fill(['firstname' => 'Bob']);
}

public function form(Form $form): Form
{
return $form
->schema([TextInput::make('firstname')])
->statePath('data'); // <- This is where you tell it to store the form information in the 'data' array
}
}
urbycoz
urbycozOP14mo ago
That's perfect. Thanks so much
Want results from more Discord servers?
Add your server