可选参数应当如何验证
控制器里的方法public function create在创建文章时,其中一个post过来的值是一个可选参数
$request->validate([
'topic' => 'required|exists:article_topics,id',
'type' => 'required|exists:article_types,id',
'title' => 'required|min:2',
'content' => 'required|min:10',
],[
'topic.*' => __('lang_Common.Enter Topic'),
'type.*' => __('lang_Common.Enter Type'),
'title.*' => __('lang_Common.Enter Title'),
'content.*' => __('lang_Common.Enter Content'),
]
);
其中的
'type' => 'required|exists:article_types,id',
当存在这个字段时是必填,不存在不填。
'sometimes|required' 表单验证《Laravel 8 中文文档》