Files
zemailnator/app/Filament/Resources/Domains/Schemas/DomainForm.php
idevakk 1c4298cdaf feat(domains): update unique constraint to composite with type and provider
- Remove single unique constraint on domain name
  - Add composite unique constraint on name, domain_type, and provider_type
  - Update Filament domain form validation with custom unique rule
  - Add descriptive validation message for duplicate domains
2025-12-08 10:57:56 -08:00

82 lines
3.3 KiB
PHP

<?php
namespace App\Filament\Resources\Domains\Schemas;
use App\enum\DomainType;
use App\enum\ProviderType;
use Filament\Forms\Components\DateTimePicker;
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
{
public static function configure(Schema $schema): Schema
{
return $schema
->components([
TextInput::make('name')
->columnSpan(1)
->helperText('Domain name: example.com')
->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)
->default(100)
->helperText('How many mailboxes can be created with this domain daily')
->columnSpan(1)
->required(),
ToggleButtons::make('is_active')
->options([
true => 'Active',
false => 'Disabled',
])
->inline()
->default(true)
->columnSpanFull()
->required(),
Select::make('domain_type')
->options(DomainType::class)
->enum(DomainType::class)
->required(),
Select::make('provider_type')
->options(ProviderType::class)
->enum(ProviderType::class)
->required(),
DateTimePicker::make('starts_at'),
DateTimePicker::make('ends_at'),
TextEntry::make('last_used_at')
->label('Last Used At')
->formatStateUsing(fn ($state) => $state ? $state->diffForHumans() : 'Never')
->visible(fn ($context) => $context === 'edit'),
TextEntry::make('checked_at')
->label('Last Checked At')
->formatStateUsing(fn ($state) => $state ? $state->diffForHumans() : 'Never')
->visible(fn ($context) => $context === 'edit'),
TextEntry::make('deleted_at')
->label('Deleted At')
->formatStateUsing(fn ($state) => $state ? $state->diffForHumans() : null)
->color('danger')
->icon('heroicon-o-trash')
->visible(fn ($record) => $record?->deleted_at !== null),
])->columns(2);
}
}