我有三個欄位
- 標題
- 身體
- 圖片[]
身體需要沒有影像,所以我做了以下事情:
$request->validate([
'title' => 'required|string|max:' . (int) Setting::getValue('titlemaxchars'),
'body' => [
'required_without:images',
'string',
'min:'.(int) Setting::getValue('postminchars'),
'max:'.(int) Setting::getValue('postmaxchars'),
],
]);
if ($request->hasFile('images')) {
foreach ($request->file('images') as $file) {
Validator::validate(['photo' => $file], [
'photo' => ['required', 'file', 'image', 'max:2048'],
]);
}
}
問題在于以下幾點:
- 正文字串
- 身體最小
- 身體最大
如果有影像,我不希望他們發出任何錯誤訊息,并且只有在沒有影像時才會出現錯誤訊息。
uj5u.com熱心網友回復:
這對我有用!
$request->validate([
'title' => 'required|string|max:' . (int) Setting::getValue('titlemaxchars'),
]);
if(!empty($request->hasFile('images'))){
$request->validate([
'body' => 'nullable',
]);
}else{
$request->validate([
'body' => [
'required',
'string',
'min:'.(int) Setting::getValue('postminchars'),
'max:'.(int) Setting::getValue('postmaxchars'),
],
]);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/387443.html
下一篇:物體框架驗證外部物體未被修改
