仅当值不可用时才需要验证

时间:2018-07-11 15:28:21

标签: laravel laravel-5

我有以下代码:

{!! Form::select('the_selection', ['' => 'Nothing', 'Foo' => 'Bar']) !!}

以及以下内容:

<input class="input" name="the_other_field" type="text"/>

仅当the_other_field不是the_selection时才需要''字段。

所以我尝试了验证:

$this->validate($request, [
    // 'recurring'        => 'required|nullable',
    'the_other_field'      => 'required_without:the_selection,|date_format:Y-m-d|after:today',
]);

但这不起作用。

2 个答案:

答案 0 :(得分:0)

如果将验证移至表单请求,则可以添加“有时”验证规则:

print(df$family) 
[1] "Rheidae"   "Rheidae"   "Tinamidae" "Tinamidae" "Tinamidae" "Tinamidae"
[7] "Tinamidae" "Tinamidae" "Tinamidae" "Tinamidae" "Tinamidae" "Tinamidae"
[13] "Tinamidae" "Tinamidae" "Tinamidae" "Tinamidae" "Tinamidae" "Tinamidae"
[19] "Tinamidae" "Tinamidae" "Tinamidae" "Tinamidae" "Tinamidae" "Tinamidae"
[25] "Tinamidae" "Tinamidae" "Tinamidae" "Tinamidae" "Tinamidae" "Tinamidae"
[31] "Tinamidae" "Tinamidae" "Tinamidae" "Tinamidae" "Tinamidae" "Tinamidae"
[37] "Tinamidae" "Tinamidae" "Tinamidae" "Tinamidae" "Tinamidae" "Tinamidae"
[43] "Tinamidae" "Tinamidae" "Tinamidae" "Tinamidae"

答案 1 :(得分:0)

使用 if(isset($_FILES['file'])){ // DO SOMETHING echo 'files received'; }else{ echo 'files not received'; }

  

https://laravel.com/docs/5.6/validation

required_if

如果这不起作用,您可以这样做:

$this->validate($request, [
   // 'recurring'         => 'required|nullable',
   'the_other_field'      => 'required_if:recurring,|required_without:the_selection,|date_format:Y-m-d|after:today',
]);