Skip to content

Commit 32a6457

Browse files
scott graysonscott grayson
authored andcommitted
Remove duplicate validation from createOptionUsing
- Form validation with UniqueTagName Rule should prevent duplicates - createOptionUsing should only be called if validation passes - Simplifies code by removing redundant validation check
1 parent 2d6295d commit 32a6457

File tree

3 files changed

+3
-57
lines changed

3 files changed

+3
-57
lines changed

src/Resources/Pages/EditFile.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,9 @@ public function form(\Filament\Schemas\Schema $schema): \Filament\Schemas\Schema
5959
->validationAttribute('tag name'),
6060
])
6161
->createOptionUsing(function (array $data): int {
62-
$slug = \Illuminate\Support\Str::slug($data['name']);
63-
64-
// Check if a tag with this slug already exists
65-
$existingTag = \Tapp\FilamentLibrary\Models\LibraryItemTag::where('slug', $slug)->first();
66-
67-
if ($existingTag) {
68-
// Re-validate to trigger form validation display
69-
\Illuminate\Support\Facades\Validator::make($data, [
70-
'name' => [
71-
function ($attribute, $value, $fail) use ($existingTag) {
72-
if ($existingTag) {
73-
$fail('A tag with this name already exists.');
74-
}
75-
},
76-
],
77-
])->validate();
78-
}
79-
8062
$tag = \Tapp\FilamentLibrary\Models\LibraryItemTag::create([
8163
'name' => $data['name'],
82-
'slug' => $slug,
64+
'slug' => \Illuminate\Support\Str::slug($data['name']),
8365
]);
8466

8567
return $tag->id;

src/Resources/Pages/EditFolder.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,9 @@ public function form(\Filament\Schemas\Schema $schema): \Filament\Schemas\Schema
4343
->validationAttribute('tag name'),
4444
])
4545
->createOptionUsing(function (array $data): int {
46-
$slug = \Illuminate\Support\Str::slug($data['name']);
47-
48-
// Check if a tag with this slug already exists
49-
$existingTag = \Tapp\FilamentLibrary\Models\LibraryItemTag::where('slug', $slug)->first();
50-
51-
if ($existingTag) {
52-
// Re-validate to trigger form validation display
53-
\Illuminate\Support\Facades\Validator::make($data, [
54-
'name' => [
55-
function ($attribute, $value, $fail) use ($existingTag) {
56-
if ($existingTag) {
57-
$fail('A tag with this name already exists.');
58-
}
59-
},
60-
],
61-
])->validate();
62-
}
63-
6446
$tag = \Tapp\FilamentLibrary\Models\LibraryItemTag::create([
6547
'name' => $data['name'],
66-
'slug' => $slug,
48+
'slug' => \Illuminate\Support\Str::slug($data['name']),
6749
]);
6850

6951
return $tag->id;

src/Resources/Pages/EditLink.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,9 @@ public function form(\Filament\Schemas\Schema $schema): \Filament\Schemas\Schema
4747
->validationAttribute('tag name'),
4848
])
4949
->createOptionUsing(function (array $data): int {
50-
$slug = \Illuminate\Support\Str::slug($data['name']);
51-
52-
// Check if a tag with this slug already exists
53-
$existingTag = \Tapp\FilamentLibrary\Models\LibraryItemTag::where('slug', $slug)->first();
54-
55-
if ($existingTag) {
56-
// Re-validate to trigger form validation display
57-
\Illuminate\Support\Facades\Validator::make($data, [
58-
'name' => [
59-
function ($attribute, $value, $fail) use ($existingTag) {
60-
if ($existingTag) {
61-
$fail('A tag with this name already exists.');
62-
}
63-
},
64-
],
65-
])->validate();
66-
}
67-
6850
$tag = \Tapp\FilamentLibrary\Models\LibraryItemTag::create([
6951
'name' => $data['name'],
70-
'slug' => $slug,
52+
'slug' => \Illuminate\Support\Str::slug($data['name']),
7153
]);
7254

7355
return $tag->id;

0 commit comments

Comments
 (0)