admin管理员组文章数量:1321046
I run a validation rule on a combination of 3 fields (f_name, s_name, l_name) to make sure the combination is unique in an institution, but the rule fails and don't apply until the third entry after passing 2 entries that have the same name combination details
public function rules(): array
{
$userId = User::where('phone', $this->phone)->value('id');
$id = $this->id ?? null;
$uniqueNameRule = function ($column, $otherColumns) use ($userId, $id) {
return [
'required',
'max:20',
Rule::unique('summonedees', $column)
->where(function ($query) use ($userId, $otherColumns) {
foreach ($otherColumns as $col => $val) {
$query->where($col, $this->summoned[$val]);
}
return $query->where('user_id', $userId)
->where('institution_id', $this->institution_id);
})->ignore($id)
];
};
$rules = [
'summoned.f_name' => $uniqueNameRule('f_name', ['s_name' => 's_name', 'l_name' => 'l_name']),
'summoned.s_name' => $uniqueNameRule('s_name', ['f_name' => 'f_name', 'l_name' => 'l_name']),
'summoned.l_name' => $uniqueNameRule('l_name', ['f_name' => 'f_name', 's_name' => 's_name']),
'summoned.branch_id' => 'required|exists:branches,id',
'phone' => ['required', 'phone:mobile', new CallerRule],
];
$rules['avatar'] = $this->isMethod('post')
? 'required|image|mimes:jpg,jpeg,png|max:100'
: 'sometimes|image|mimes:jpg,jpeg,png|max:100';
return $rules;
}
版权声明:本文标题:laravel - Validation request fails on the second entry and starts to work on the third entry - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742094008a2420434.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论