Skip to content

Commit dd9853d

Browse files
scott graysonscott grayson
authored andcommitted
Fix: Add validation for duplicate tags when creating new tags
- Add validation rule to TextInput in createOptionForm to check for duplicate tag slugs - Validation error now displays inline on the form field - Prevents SQL errors when attempting to create duplicate tags - Applied to EditFile, EditLink, and EditFolder pages
1 parent 5833ad0 commit dd9853d

3 files changed

Lines changed: 33 additions & 3 deletions

File tree

src/Resources/Pages/EditFile.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,17 @@ public function form(\Filament\Schemas\Schema $schema): \Filament\Schemas\Schema
5353
->createOptionForm([
5454
\Filament\Forms\Components\TextInput::make('name')
5555
->required()
56-
->maxLength(255),
56+
->maxLength(255)
57+
->rules([
58+
function ($attribute, $value, $fail) {
59+
$slug = \Illuminate\Support\Str::slug($value);
60+
$existingTag = \Tapp\FilamentLibrary\Models\LibraryItemTag::where('slug', $slug)->first();
61+
62+
if ($existingTag) {
63+
$fail('A tag with this name already exists.');
64+
}
65+
},
66+
]),
5767
])
5868
->createOptionUsing(function (array $data): int {
5969
$tag = \Tapp\FilamentLibrary\Models\LibraryItemTag::create([

src/Resources/Pages/EditFolder.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,17 @@ public function form(\Filament\Schemas\Schema $schema): \Filament\Schemas\Schema
3737
->createOptionForm([
3838
\Filament\Forms\Components\TextInput::make('name')
3939
->required()
40-
->maxLength(255),
40+
->maxLength(255)
41+
->rules([
42+
function ($attribute, $value, $fail) {
43+
$slug = \Illuminate\Support\Str::slug($value);
44+
$existingTag = \Tapp\FilamentLibrary\Models\LibraryItemTag::where('slug', $slug)->first();
45+
46+
if ($existingTag) {
47+
$fail('A tag with this name already exists.');
48+
}
49+
},
50+
]),
4151
])
4252
->createOptionUsing(function (array $data): int {
4353
$tag = \Tapp\FilamentLibrary\Models\LibraryItemTag::create([

src/Resources/Pages/EditLink.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,17 @@ public function form(\Filament\Schemas\Schema $schema): \Filament\Schemas\Schema
4141
->createOptionForm([
4242
\Filament\Forms\Components\TextInput::make('name')
4343
->required()
44-
->maxLength(255),
44+
->maxLength(255)
45+
->rules([
46+
function ($attribute, $value, $fail) {
47+
$slug = \Illuminate\Support\Str::slug($value);
48+
$existingTag = \Tapp\FilamentLibrary\Models\LibraryItemTag::where('slug', $slug)->first();
49+
50+
if ($existingTag) {
51+
$fail('A tag with this name already exists.');
52+
}
53+
},
54+
]),
4555
])
4656
->createOptionUsing(function (array $data): int {
4757
$tag = \Tapp\FilamentLibrary\Models\LibraryItemTag::create([

0 commit comments

Comments
 (0)