diff --git a/app/Filament/Resources/Domains/Schemas/DomainForm.php b/app/Filament/Resources/Domains/Schemas/DomainForm.php index c687f98..fa7abb5 100644 --- a/app/Filament/Resources/Domains/Schemas/DomainForm.php +++ b/app/Filament/Resources/Domains/Schemas/DomainForm.php @@ -9,7 +9,9 @@ use Filament\Forms\Components\Select; use Filament\Forms\Components\TextInput; use Filament\Forms\Components\ToggleButtons; use Filament\Infolists\Components\TextEntry; +use Filament\Schemas\Components\Utilities\Get; use Filament\Schemas\Schema; +use Illuminate\Validation\Rules\Unique; class DomainForm { @@ -20,7 +22,19 @@ class DomainForm TextInput::make('name') ->columnSpan(1) ->helperText('Domain name: example.com') - ->required(), + ->required() + ->unique( + table: 'domains', + ignoreRecord: true, + modifyRuleUsing: function (Unique $rule, Get $get) { + return $rule + ->where('domain_type', $get('domain_type')) + ->where('provider_type', $get('provider_type')); + }, + ) + ->validationMessages([ + 'unique' => 'The domain name already exists for this type and provider.', + ]), TextInput::make('daily_mailbox_limit') ->integer() ->minValue(1) diff --git a/database/migrations/2025_12_08_174044_update_domains_unique_constraint_remove_single_add_composite.php b/database/migrations/2025_12_08_174044_update_domains_unique_constraint_remove_single_add_composite.php new file mode 100644 index 0000000..42302f3 --- /dev/null +++ b/database/migrations/2025_12_08_174044_update_domains_unique_constraint_remove_single_add_composite.php @@ -0,0 +1,37 @@ +dropUnique('domains_name_unique'); + + // Add a composite unique constraint on name, domain_type, and provider_type + // This allows the same domain name with different type combinations + $table->unique(['name', 'domain_type', 'provider_type'], 'domains_name_type_provider_unique'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('domains', function (Blueprint $table) { + // Drop the composite unique constraint + $table->dropUnique('domains_name_type_provider_unique'); + + // Restore the original single unique constraint on 'name' + $table->unique('name', 'domains_name_unique'); + }); + } +};