Skip to content

Commit cc66c45

Browse files
committed
enhance: Add distinct validation on "field" before add to "field group"
1 parent 3679d39 commit cc66c45

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

src/Filament/Resources/Helpers/FieldGroupResourceHelper.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Filament\Support\Enums\ActionSize;
88
use Filament\Support\Facades\FilamentIcon;
99
use Illuminate\Support\Str;
10+
use Illuminate\Validation\ValidationException;
1011
use SolutionForest\InspireCms\Helpers\UIHelper;
1112
use SolutionForest\InspireCms\InspireCmsConfig;
1213

@@ -166,6 +167,21 @@ protected static function configureFieldsCreateActionOnRepeater(Forms\Components
166167
'name' => $modelName,
167168
]))
168169
->modalSubmitActionLabel(__('inspirecms::buttons.add.label'))
170+
->before(function (array $data, Forms\Components\Repeater $component) {
171+
// Add Uniqiue name validation
172+
$exisingState = collect($component->getState());
173+
// Get the existing names from the repeater state (can get form data from the action aftger original form validation)
174+
$nameToCheck = $data['name'] ?? null;
175+
176+
// If the name is not empty and already exists in the existing names
177+
// throw a validation exception
178+
if ($nameToCheck && $exisingState->contains('name', $nameToCheck)) {
179+
$validationAttribute = __('inspirecms::resources/field.name.validation_attribute');
180+
throw ValidationException::withMessages([
181+
'mountedFormComponentActionsData.0.name' => __('validation.distinct', ['attribute' => $validationAttribute]),
182+
]);
183+
}
184+
})
169185
->action(function (array $data, Forms\Components\Repeater $component) {
170186
$newUuid = $component->generateUuid();
171187

0 commit comments

Comments
 (0)