最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

laravel - Validation request fails on the second entry and starts to work on the third entry - Stack Overflow

programmeradmin7浏览0评论

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;
    }
发布评论

评论列表(0)

  1. 暂无评论