首页 > 解决方案 > Required_with 验证数组

问题描述

使用 required_with 验证验证数组时遇到问题。即使我什么也没输入,它总是显示。从两天前开始我就收到了这个错误。我想检查一个字段是否插入值,另一个字段必须插入值。如果没有插入,则另一个字段不需要插入。请问你能帮帮我吗?

这是我得到的错误,即使我输入值与否。有什么解决方案吗? 错误信息图片

验证请求代码。

        'work_procedure[]'=>'nullable|required_with:times,work_points',
        'times[]'=>'nullable|required_with:work_procedure',
        'work_points[]'=>'nullable|required_with:work_procedure',

这是 Model.php

  protected $fillable=[

   'work_procedure',
   'times',
   'work_points',

];

标签: validationlaravel-5.8

解决方案


https://laravel.com/docs/8.x/validation#rule-required-with-all

required_with_all:foo,bar,...

仅当所有其他指定字段都存在且不为空时,验证中的字段必须存在且不为空。

required_without:foo,bar,...

仅当任何其他指定字段为空或不存在时,验证字段必须存在且不为空。

required_without_all:foo,bar,...

仅当所有其他指定字段为空或不存在时,验证字段必须存在且不为空。

例子

   $request->validate([
            'name' => 'required',
            'surname' => 'required_with:name'
   ]);
  $request->validate([
          'name' => 'required',
          'detail' => 'required|required_with:name',
  ]);
   Validator::make($request->all(), [
     'role_id' => Rule::required_with_all:foo,bar,
   ]);


推荐阅读