F
Filament2mo ago
Arjen

$this->form->getState() empty

Hi, I've created a Filament page with the make command and I've placed a form inside the component. When I submit my form, $this->form->getState() returns an empty array and I'm not sure why. The $data property however is filled correctly, and my validation also runs fine. Any idea what's wrong with my code? Or shouldn't I use $this->form->getState() and just use $this->data? My file:
class VolunteerSettings extends Page implements HasForms
{
use InteractsWithFormActions;
use InteractsWithForms;

protected static ?string $navigationIcon = 'heroicon-o-document-text';

protected static string $view = 'filament.pages.volunteer-settings';

protected static ?string $slug = 'vrijwilligervoorkeuren';

protected static ?string $title = 'Vrijwilligervoorkeuren';

public ?array $data = [];

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

public function form(Form $form): Form
{
return $form
->model(auth()->user())
->schema([
Forms\Components\Section::make()
->relationship('volunteerProfile')
->schema([
'is_active' => Forms\Components\Toggle::make('is_active')
->label('Actief profiel')
->default(false)
->live(),
// ...
]),
])
->statePath('data');
}

public function getFormActions(): array
{
return [
Actions\Action::make('submit')
->label('Opslaan')
->submit('submit'),
];
}

public function submit()
{
$data = $this->form->getState();
dd($data, $this->data);
}
}
class VolunteerSettings extends Page implements HasForms
{
use InteractsWithFormActions;
use InteractsWithForms;

protected static ?string $navigationIcon = 'heroicon-o-document-text';

protected static string $view = 'filament.pages.volunteer-settings';

protected static ?string $slug = 'vrijwilligervoorkeuren';

protected static ?string $title = 'Vrijwilligervoorkeuren';

public ?array $data = [];

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

public function form(Form $form): Form
{
return $form
->model(auth()->user())
->schema([
Forms\Components\Section::make()
->relationship('volunteerProfile')
->schema([
'is_active' => Forms\Components\Toggle::make('is_active')
->label('Actief profiel')
->default(false)
->live(),
// ...
]),
])
->statePath('data');
}

public function getFormActions(): array
{
return [
Actions\Action::make('submit')
->label('Opslaan')
->submit('submit'),
];
}

public function submit()
{
$data = $this->form->getState();
dd($data, $this->data);
}
}
2 Replies
LeandroFerreira
LeandroFerreira2mo ago
if you want to get the relationship data using getState(), add ->dehydrated(true) after ->relationship()

Did you find this page helpful?