F
Filament15mo ago
Finn

Disabled field through $set function not updating state

I used the Filament documentation (https://filamentphp.com/docs/3.x/forms/advanced#generating-a-slug-from-a-title) to automatically generate a slug based on a text field. Like this:
TextInput::make('name')
->label('Naam')
->required()
->live()
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state): void {
if (($get('slug') ?? '') !== Str::slug($old)) {
return;
}

$set('slug', Str::slug($state));
}),

TextInput::make('slug')
->prefix('https://test.nl/categorie/')
->suffix('.nl')
->disabled(),
TextInput::make('name')
->label('Naam')
->required()
->live()
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state): void {
if (($get('slug') ?? '') !== Str::slug($old)) {
return;
}

$set('slug', Str::slug($state));
}),

TextInput::make('slug')
->prefix('https://test.nl/categorie/')
->suffix('.nl')
->disabled(),
As you can see I am disabling the slug TextInput, because I dont want users to be able to modify it as it automatically does that. When I click on save I get an error that the slug column cannot be inserted in my database because the value is null. But it does have a value think If I remove the ->disabled() from the slug TextInput it does save fine.
2 Replies
Vp
Vp15mo ago
Finn
FinnOP15mo ago
Thanks! Works :)
Want results from more Discord servers?
Add your server