php Laravel 9和Livewire唯一性确认(除非更新)

ki0zmccv  于 2022-10-30  发布在  PHP
关注(0)|答案(2)|浏览(153)

下面的验证在创建新记录时有效,但在更新记录时,partner_codeseedgens_codeunique验证中被捕获。如何允许记录在未更改的情况下使用相同的值进行更新,但在值确实更改时仍验证其唯一性?

$this->validate(
    [
        'partner_code' => 'required|unique:varieties',
        'seedgens_code' => 'required|unique:varieties',
    ],
    [
        'partner_code.required' => 'Please add a partner code.',
        'partner_code.unique' => 'Partner code must be unique.',
        'seedgens_code.required' => 'Please add a unique partner code.',
        'seedgens_code.unique' => 'SeedGens code must be unique.',
    ],
);
tez616oj

tez616oj1#

'partner_code' => 'required|unique:varieties,' . $id

'partner_code' => ['required', Rule::unique('varieties')->ignore($id)]

其中$id是要忽略的ID。
https://laravel.com/docs/9.x/validation#rule-unique

g6ll5ycj

g6ll5ycj2#

(1) protected $rules=[
    'partner_code' => ['required', Rule::unique('varieties')->ignore($id)]
    ....
]

(2) protected function rules(){
    'partner_code' => ['required', Rule::unique('varieties')->ignore($id)]
    ...
}

我将规则添加到(1)不起作用我将规则添加到(2)起作用!!!

相关问题