Skip to content

Commit d85d862

Browse files
scott graysonscott grayson
authored andcommitted
Add validation in createOptionUsing callback as fallback
- Add duplicate tag check in createOptionUsing callback - This ensures validation runs even if form validation doesn't trigger - Prevents SQL errors when creating duplicate tags
1 parent dd9853d commit d85d862

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/Resources/Pages/EditFile.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,20 @@ function ($attribute, $value, $fail) {
6666
]),
6767
])
6868
->createOptionUsing(function (array $data): int {
69+
$slug = \Illuminate\Support\Str::slug($data['name']);
70+
71+
// Check if a tag with this slug already exists
72+
$existingTag = \Tapp\FilamentLibrary\Models\LibraryItemTag::where('slug', $slug)->first();
73+
74+
if ($existingTag) {
75+
throw \Illuminate\Validation\ValidationException::withMessages([
76+
'name' => ['A tag with this name already exists.'],
77+
]);
78+
}
79+
6980
$tag = \Tapp\FilamentLibrary\Models\LibraryItemTag::create([
7081
'name' => $data['name'],
71-
'slug' => \Illuminate\Support\Str::slug($data['name']),
82+
'slug' => $slug,
7283
]);
7384

7485
return $tag->id;

src/Resources/Pages/EditFolder.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,20 @@ function ($attribute, $value, $fail) {
5050
]),
5151
])
5252
->createOptionUsing(function (array $data): int {
53+
$slug = \Illuminate\Support\Str::slug($data['name']);
54+
55+
// Check if a tag with this slug already exists
56+
$existingTag = \Tapp\FilamentLibrary\Models\LibraryItemTag::where('slug', $slug)->first();
57+
58+
if ($existingTag) {
59+
throw \Illuminate\Validation\ValidationException::withMessages([
60+
'name' => ['A tag with this name already exists.'],
61+
]);
62+
}
63+
5364
$tag = \Tapp\FilamentLibrary\Models\LibraryItemTag::create([
5465
'name' => $data['name'],
55-
'slug' => \Illuminate\Support\Str::slug($data['name']),
66+
'slug' => $slug,
5667
]);
5768

5869
return $tag->id;

src/Resources/Pages/EditLink.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,20 @@ function ($attribute, $value, $fail) {
5454
]),
5555
])
5656
->createOptionUsing(function (array $data): int {
57+
$slug = \Illuminate\Support\Str::slug($data['name']);
58+
59+
// Check if a tag with this slug already exists
60+
$existingTag = \Tapp\FilamentLibrary\Models\LibraryItemTag::where('slug', $slug)->first();
61+
62+
if ($existingTag) {
63+
throw \Illuminate\Validation\ValidationException::withMessages([
64+
'name' => ['A tag with this name already exists.'],
65+
]);
66+
}
67+
5768
$tag = \Tapp\FilamentLibrary\Models\LibraryItemTag::create([
5869
'name' => $data['name'],
59-
'slug' => \Illuminate\Support\Str::slug($data['name']),
70+
'slug' => $slug,
6071
]);
6172

6273
return $tag->id;

0 commit comments

Comments
 (0)