|
7 | 7 | use Filament\Support\Enums\ActionSize; |
8 | 8 | use Filament\Support\Facades\FilamentIcon; |
9 | 9 | use Illuminate\Support\Str; |
| 10 | +use Illuminate\Validation\ValidationException; |
10 | 11 | use SolutionForest\InspireCms\Helpers\UIHelper; |
11 | 12 | use SolutionForest\InspireCms\InspireCmsConfig; |
12 | 13 |
|
@@ -166,6 +167,21 @@ protected static function configureFieldsCreateActionOnRepeater(Forms\Components |
166 | 167 | 'name' => $modelName, |
167 | 168 | ])) |
168 | 169 | ->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 | + }) |
169 | 185 | ->action(function (array $data, Forms\Components\Repeater $component) { |
170 | 186 | $newUuid = $component->generateUuid(); |
171 | 187 |
|
|
0 commit comments