Patrick | Der Echte!
Patrick | Der Echte!
FFilament
Created by Patrick | Der Echte! on 8/17/2024 in #❓┊help
afterStateUpdated $old does not work after using Builder/Block
Hey, i am currently trying to use afterStateUpdatedon a TextInput in combination with a Builder. The resource i am working on does have a *title *and a *slug *attribute that i have implemented as described in the documentation as well as a Builder. My issue is that after i have used any field inside a Block, the the $old parameter of the afterStateUpdated callback does equal the new value and not the old value. However, if i use any other field outside of the Builder, like test, before i change the title, it does work again. I am using the following code:
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('test'),
Forms\Components\TextInput::make('title')
->required()
->maxLength(255)
->live(onBlur: true)
->afterStateUpdated(function (Forms\Get $get, Forms\Set $set, ?string $old, ?string $state) {
if (($get('slug') ?? '') !== Str::slug($old)) {
return;
}

$set('slug', Str::slug($state));
}),
Forms\Components\TextInput::make('slug')
->required()
->maxLength(255),

Forms\Components\Builder::make('data')
->blocks([
Builder\Block::make('heading')
->schema([
Components\TextInput::make('content')
->label('Heading')
->required(),
Components\Select::make('level')
->options([
'h1' => 'Heading 1',
'h2' => 'Heading 2',
'h3' => 'Heading 3',
'h4' => 'Heading 4',
'h5' => 'Heading 5',
'h6' => 'Heading 6',
])
->required()
])
->columns(2);
])
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('test'),
Forms\Components\TextInput::make('title')
->required()
->maxLength(255)
->live(onBlur: true)
->afterStateUpdated(function (Forms\Get $get, Forms\Set $set, ?string $old, ?string $state) {
if (($get('slug') ?? '') !== Str::slug($old)) {
return;
}

$set('slug', Str::slug($state));
}),
Forms\Components\TextInput::make('slug')
->required()
->maxLength(255),

Forms\Components\Builder::make('data')
->blocks([
Builder\Block::make('heading')
->schema([
Components\TextInput::make('content')
->label('Heading')
->required(),
Components\Select::make('level')
->options([
'h1' => 'Heading 1',
'h2' => 'Heading 2',
'h3' => 'Heading 3',
'h4' => 'Heading 4',
'h5' => 'Heading 5',
'h6' => 'Heading 6',
])
->required()
])
->columns(2);
])
]);
}
Any idea what could be wrong?
4 replies