首页 > 解决方案 > 如何验证 laravel 中的空数组选择框?

问题描述

我正在使用 laravel 开发一个需要一系列选择选项的应用程序

我的代码

   <select name="test_id[]" class="form-control select-test-{{$idselect}}"
     id="select-{{Illuminate\Support\Str::random(10)}}">
     <option value="">Select Patient Test</option>
     @foreach ($tests as $item)
         <option value="{{$item->id}}">{{$item->name}}</option>
     @endforeach
   </select>

Laravel 验证码

 $request->validate([
        'test_id' => 'required:array'
    ], [
       
        'test_id.required' => "Test is required"
    ]);

即使没有选择它也会通过

未选中时输出

在此处输入图像描述

标签: htmllaravelselect

解决方案


那么您唯一缺少的是multiple您的选择元素中的属性。

<select multiple name="test_id[]" class="form-control select-test-{{$idselect}}" id="select-{{Illuminate\Support\Str::random(10)}}">

推荐阅读