F
Filament12mo ago
Abi

Composite Key validation when saving a record

I have the following components on the form of relation manager class
public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('option_1_value')
->relationship('option1', 'key')
->required(),

Forms\Components\Select::make('option_2_value')
->relationship('option2', 'key')
->required(),

Forms\Components\Select::make('option_3_value')
->relationship('option3', 'key')
->required(),
]);
}
public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('option_1_value')
->relationship('option1', 'key')
->required(),

Forms\Components\Select::make('option_2_value')
->relationship('option2', 'key')
->required(),

Forms\Components\Select::make('option_3_value')
->relationship('option3', 'key')
->required(),
]);
}
How can I validate unique combination selection for the parent resource when saving the form?
10 Replies
Abi
AbiOP12mo ago
Basically, the selection for option 1 / option 2 / option 3 needs to be unique for the parent relation any advice on this please? @awcodes any advice on this.
awcodes
awcodes12mo ago
maybe?
->unique(column: ['option_1_value', 'option_2_value', 'option_3_value'])
->unique(column: ['option_1_value', 'option_2_value', 'option_3_value'])
Abi
AbiOP12mo ago
where do I set this?
Abi
AbiOP12mo ago
so do I add this rule to all the 3 fields?
public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('option_1_value')
->relationship('option1', 'key')
->unique(column: ['option_1_value', 'option_2_value', 'option_3_value'])
->required(),

Forms\Components\Select::make('option_2_value')
->relationship('option2', 'key')
->unique(column: ['option_1_value', 'option_2_value', 'option_3_value'])
->required(),

Forms\Components\Select::make('option_3_value')
->relationship('option3', 'key')
->unique(column: ['option_1_value', 'option_2_value', 'option_3_value'])
->required(),
]);
}
public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('option_1_value')
->relationship('option1', 'key')
->unique(column: ['option_1_value', 'option_2_value', 'option_3_value'])
->required(),

Forms\Components\Select::make('option_2_value')
->relationship('option2', 'key')
->unique(column: ['option_1_value', 'option_2_value', 'option_3_value'])
->required(),

Forms\Components\Select::make('option_3_value')
->relationship('option3', 'key')
->unique(column: ['option_1_value', 'option_2_value', 'option_3_value'])
->required(),
]);
}
awcodes
awcodes12mo ago
don't know, haven't needed it. just try it. it is confusing though, not gonna lie you're trying to unique across tables though, so i'm not sure there is a good way to do this.
Abi
AbiOP12mo ago
the problem with the above is, the uniqueness for the parent id isn't set the table has
parent_id, option_1_value, option_2_value, option_3_value
parent_id, option_1_value, option_2_value, option_3_value
these are the 4 columns and I am trying to find option value combination for the parent_id
1, 1,2,3
1, 2,1,3
1, 3,2,1
1, 1,3,2
1, 2,3,1
2, 1,2,3
....
1, 1,2,3
1, 2,1,3
1, 3,2,1
1, 1,3,2
1, 2,3,1
2, 1,2,3
....
awcodes
awcodes12mo ago
yea, i'm understanding. You're probably going to have to do a custom rule for this. https://laraveldaily.com/post/laravel-unique-validation-multiple-columns
awcodes
awcodes12mo ago
but it'll probably get fairly complicated with each 'option' being a separate relationship and table. as long as the query is correct it should work though.
Abi
AbiOP12mo ago
ok, let me give it a try
Want results from more Discord servers?
Add your server