F
Filament3w ago
sven

afterStateUpdated in toggle in a custom component not firering

What I want to do I want to have a toggle in my profilenav to switch to an expert mode for extended options. I am using the formbuilder not for its functionality but because of the consistent look and feel / easy way of getting a toggle. What is happening After refreshing the page the toggle is still turned off, even if I interacted with it. When toggleing I do not see any network activity, so I think that toggleing just doesnt do anything. My code
<?php

namespace App\Livewire;

use Filament\Forms;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Filament\Forms\Set;
use Livewire\Component;

class ExpertModeSwitcher extends Component implements HasForms
{
use InteractsWithForms;

public ?array $data = [];

public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Toggle::make('active')
->label('Expertenmodus')
->default(session('expertMode'))
->live(onBlur: true)
->afterStateUpdated(fn (Set $set, ?string $state) =>
session(['expertMode' => $state])
)
])
->statePath('data');
}

public function render()
{
return view('livewire.expert-mode-switcher');
}
}
<?php

namespace App\Livewire;

use Filament\Forms;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Filament\Forms\Set;
use Livewire\Component;

class ExpertModeSwitcher extends Component implements HasForms
{
use InteractsWithForms;

public ?array $data = [];

public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Toggle::make('active')
->label('Expertenmodus')
->default(session('expertMode'))
->live(onBlur: true)
->afterStateUpdated(fn (Set $set, ?string $state) =>
session(['expertMode' => $state])
)
])
->statePath('data');
}

public function render()
{
return view('livewire.expert-mode-switcher');
}
}
Solution:
add $this->form->fill() to the mount method
Jump to solution
5 Replies
Matthew
Matthew3w ago
Have you verified the afterStateUpdated is definitely not firing by chucking in a dd? ->live(onBlur: true) I don't think you need onBlur: true here
sven
svenOP3w ago
@Matthew yeah, I tried that but that also didnt seem to do anything
Solution
LeandroFerreira
add $this->form->fill() to the mount method
LeandroFerreira
Filament
Form Builder - Common Errors to Avoid by Leandro Ferreira - Filament
A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
sven
svenOP3w ago
Ahhh.. Thank you so much!

Did you find this page helpful?