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 AliOP11mo ago
do you need to more details any knowledge?
Thijmen
Thijmen11mo ago
This isnt filament related right?
Ahmed Ali
Ahmed AliOP11mo 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
Tieme11mo 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 AliOP11mo ago
yes this for one column what if two column is unique at same time i dont no if my question is correct
Tieme
Tieme11mo 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 AliOP11mo 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(),
Want results from more Discord servers?
Add your server