two column as unique

i have this form return $form ->schema([ Forms\Components\Select::make('program_id') ->relationship('program', 'name') ->required(), Forms\Components\Select::make('applicant_id') ->relationship('applicant', 'name') ->required(), Forms\Components\TextInput::make('values') ->required(), Forms\Components\Select::make('status_id') ->relationship('status', 'name') ->required(), ]); i need program_id and applicant _id as unique in table at same time
7 Replies
Ahmed Ali
Ahmed Ali5mo ago
do you need to more details any knowledge?
Thijmen
Thijmen5mo ago
This isnt filament related right?
Ahmed Ali
Ahmed Ali5mo ago
no its filament how in filament form i take program_id and applicant_id unique at same time in database is unique but filament how will do to ckeck
Tieme
Tieme5mo ago
Look at : https://filamentphp.com/docs/3.x/forms/validation#unique
->unique(modifyRuleUsing: function (Unique $rule) {
return $rule->where('is_active', 1);
})
->unique(modifyRuleUsing: function (Unique $rule) {
return $rule->where('is_active', 1);
})
You can create this by modifyRuleUsing
Ahmed Ali
Ahmed Ali5mo ago
yes this for one column what if two column is unique at same time i dont no if my question is correct
Tieme
Tieme5mo ago
You can also create you own rule : https://filamentphp.com/docs/3.x/forms/validation#custom-rules Something like this i think
TextInput::make('slug')->rules([
fn (Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) {
$Test1 = Model::where('column1', $get('other_field'))->exists()
$Test2 = Model::where('column2', $value))->exists()
if ($Test1 OR $Test2) {
$fail("The {$attribute} is invalid.");
}
},
])
TextInput::make('slug')->rules([
fn (Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) {
$Test1 = Model::where('column1', $get('other_field'))->exists()
$Test2 = Model::where('column2', $value))->exists()
if ($Test1 OR $Test2) {
$fail("The {$attribute} is invalid.");
}
},
])
Ahmed Ali
Ahmed Ali5mo ago
this mean i will the same for the two ones but in your condition the correct is or or and because i need the two column with same values exit as unique in DB Forms\Components\Select::make('program_id') ->relationship('program', 'name') ->required(), Forms\Components\Select::make('applicant_id') ->relationship('applicant', 'name') ->required(),