Laravel 根据另一个字段的值验证该字段

tay*_*eed 5 php validation laravel

我想验证两个字段,即“类型”和“选项”,其中“类型”字段是枚举。仅当 'type' 字段的值为 'opt' 时,才应验证 'options' 字段。

$this->validate($request, [
    'type' => 'required|in:opt,number,text,file,image',
    'options'=>the condition I need(if type is 'opt')
]);
Run Code Online (Sandbox Code Playgroud)

Smi*_*val 6

您可以在 Laravel 中使用 required_if 验证。

$this->validate($request, [
    'type' => 'required|in:opt,number,text,file,image',
    'options'=> 'required_if:type,==,opt'
]);
Run Code Online (Sandbox Code Playgroud)

这是一个文档链接