I've a situation where name field should be unique but the rule applies only when the group value matches with previously added group in the database. There's an input for group and other inputs for name which can be dynamically added by user but for simplicity I've added 2 inputs with name[] attribute below. If the 1st name input (2nd name input is not used) is sent to validation it works, however when both the input values are sent, the validation doesn't work. How can I fix it. Thanks in advance.
$request->validate([
'group' => 'required',
'name' => [
'required', Rule::unique('group_tbl')->where(function ($query) use ($request){
$query->where('group', $request->get('group'));
})
]
]);
form inputs:
<input name="group" placeholder="group here" required >
<input name="name[]" placeholder="name here" required >
<input name="name[]" placeholder="name here" >
For instance, there is a group "G" and name "A" added in a db. Now if group input "G" and name input "A" are submitted, it shows validation error "The name has already been taken." However if group input "G", name input "A" and other name input "B" are submitted, there is no error. It should not be saved since there is name "A" under group "G" already stored in db.
database:
| id | group | name |
|---|---|---|
| 1 | G | A |