所以我有upload_media
和url
。 upload_media
是您必须上载.pdf文件的地方,如果需要URL,则必须url
上载。
所以我想要的是,如果upload_media
不为空,则不需要url
。如果用户决定使用url
,则不需要upload_media
。但是,如果它们中的任何一个都没有值,那么它应该返回所需的错误。
这是我的验证:
$this->validate($request, [
'title' => 'required',
'viewing_time' => 'required',
'tags' => '',
'description' => '',
'organization' => '',
'upload_media' => '',
'url' => '',
'upload_preview' => 'required|file|image'
]);
答案 0 :(得分:5)
您可以使用required_without
规则:https://laravel.com/docs/5.6/validation#rule-required-without
$this->validate($request, [
'title' => 'required',
'viewing_time' => 'required',
'tags' => '',
'description' => '',
'organization' => '',
'upload_media' => 'required_without:url',
'url' => 'required_without:upload_media',
'upload_preview' => 'required|file|image'
]);
答案 1 :(得分:0)
$rules = [
'title' => 'required',
'viewing_time' => 'required',
'tags' => '',
'description' => '',
'organization' => '',
'upload_media' => 'required',
'url' => 'required',
'upload_preview' => 'required|file|image'
];
if ($request->file('upload_media')->isValid()) {
$rules['url'] = '';
} elseif (!empty($request->get('url'))) {
$rules['upload_media'] = '';
}
$this->validate($request, $rules);