Generate slugs from a title whilst typing (without overriding)
Sometimes you want your users to input a title (e.g. for a blog post) and automatically generate a slug from that title. However, you want to stop overriding the slug when the user has made at least one manual change.
Luckily, this is quite easy to achieve with a hidden field, to store an addition value:
TextInput::make('title') ->afterStateUpdated(function (Closure $get, Closure $set, ?string $state) { if (! $get('is_slug_changed_manually') && filled($state)) { $set('slug', Str::slug($state)); } }) ->reactive() ->required(),TextInput::make('slug') ->afterStateUpdated(function (Closure $set) { $set('is_slug_changed_manually', true); }) ->required(),Hidden::make('is_slug_changed_manually') ->default(false) ->dehydrated(false),
No comments yet…