[英]Laravel 9 the field must be a file even though I said it can be nullable (validation)
我在驗證 class 中有這個 function:
public function rules(): array {
return [
// ...
'content_image' => 'nullable|file|mimetypes:png|max:2000',
// ...
];
}
我 state,這可以是 null,但如果不是,它必須是 PNG 文件,最大大小為 2mb
似乎很簡單:
來自 Api 調用的 FormData 請求:
content: <p>1</p>
content_image: null // => Should be allowed
live_wire_component: null
page_name: test-page
page_id: 27
order: 1
驗證說不:
{
"content_image": [
"The content image must be a file.",
"Images can only be PNG"
]
}
我 900% 確定這是您通過驗證允許字段為 null 的方式:
可為空的
正在驗證的字段可能是 null。
那么即使我說它可以是 null,為什么還要圖像呢?
在您的請求驗證中將 content_image 屬性更改為此。
'content_image' => 'nullable|file|mime:png|max:2000',
我希望它有用
嘗試使用上面評論中提到的@SergheiLeonenco 的有時規則https://laravel.com/docs/9.x/validation#validating-when-present另請注意我修改了“模仿”
public function rules(): array {
return [
// ...
'content_image' => 'sometimes|file|mimes:png|max:2000',
// ...
];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.