【发布时间】:2022-01-05 05:42:03
【问题描述】:
大家好,我正在尝试验证该类别是否属于某个用户,我已经尝试过了,但是当我输入一个不存在的值时出现错误。我尝试了什么:
'category' => ['nullable','exists:categories,id', function ($attribute, $value, $fail) {
$category = Category::where('id', $value)->first();
if($category->vcard!= $this->vcard->phone_number)
{
$fail('The selected category is invalid.');
}
}]
所以当我输入一个有效的类别时这有效,但是如果我输入了一个错误的类别,例如不存在的类别 id 100000,就会在邮递员上抛出这个错误:
我认为'exists:categories,id' 会修复我,不存在的 ID。
作品:
{
"payment_reference": "a1@mail.com",
"payment_type": "PAYPAL",
"type": "D",
"value": 10,
"description": "Teste",
"category": 500
}
不工作:
{
"payment_reference": "a1@mail.com",
"payment_type": "PAYPAL",
"type": "D",
"value": 10,
"description": "Teste",
"category": 1000000
}
【问题讨论】:
标签: laravel validation