Skip to content

Commit 4f50cf9

Browse files
committed
update auto-slug logic
1 parent 1afbdf9 commit 4f50cf9

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

app/Filament/Resources/ArticleResource.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,29 @@ public static function form(Form $form): Form
3737
->schema([
3838
TextInput::make('title')
3939
->required()
40-
->maxLength(255),
40+
->maxLength(255)
41+
->live(onBlur: true)
42+
->afterStateUpdated(function (Article $article, Set $set, ?string $state) {
43+
if ($article->isPublished()) {
44+
return;
45+
}
46+
47+
$set('slug', Str::slug($state));
48+
}),
4149

4250
TextInput::make('slug')
4351
->required()
4452
->maxLength(255)
4553
->live(onBlur: true)
4654
->unique(Article::class, 'slug', ignoreRecord: true)
47-
->afterStateUpdated(fn (Set $set, ?string $state) => $set('slug', Str::slug($state))),
55+
->disabled(fn (Article $article) => $article->isPublished())
56+
->afterStateUpdated(
57+
fn (Set $set, ?string $state) => $set('slug', Str::slug($state))
58+
)
59+
->helperText(fn (Article $article) => $article->isPublished()
60+
? 'The slug cannot be changed after the article is published.'
61+
: false
62+
),
4863

4964
Textarea::make('excerpt')
5065
->required()

0 commit comments

Comments
 (0)