Php Laravel 5.4-下拉列表的自定义消息

Php Laravel 5.4-下拉列表的自定义消息,php,laravel,validation,html-select,dropdown,Php,Laravel,Validation,Html Select,Dropdown,我可以为文本字段创建验证自定义消息,但不能为文本字段创建验证自定义消息 以下是“单位”字段的HTML: 如果我将“单位”字段留空或不遵循max:2规则,则消息将是它应该显示的内容:“单位不能为空…” 但如果我将名为visitor_id的下拉列表的值保留为14,则错误消息将是默认消息“所选visitor id无效”,而不是我需要的消息;“请从列表中选择…” 所附图像显示了结果 不幸的是,我研究了一个多小时,没有找到解决办法 当然,这将是默认消息,因为您只为必需的而不是不在中更改,因此为了使其工作,

我可以为文本字段创建验证自定义消息,但不能为文本字段创建验证自定义消息

以下是“单位”字段的HTML:

如果我将“单位”字段留空或不遵循max:2规则,则消息将是它应该显示的内容:“单位不能为空…”

但如果我将名为visitor_id的下拉列表的值保留为14,则错误消息将是默认消息“所选visitor id无效”,而不是我需要的消息;“请从列表中选择…”

所附图像显示了结果

不幸的是,我研究了一个多小时,没有找到解决办法


当然,这将是默认消息,因为您只为
必需的
而不是
不在
中更改,因此为了使其工作,请在下面添加以下行:

$rules = [
   'unit'=> 'required|min:2|max:2',
   'visitor_id'=> 'required|not_in:14'
]

 $customMessages = [
    'unit.required' => 'Unit cannot be blank and must have 2 char max',
    'visitor_id.required' => 'Please choose from the list...'
    'visitor_id.not_in' => 'Please choose from the list...' 
    ];

$this->validate($request, $rules, $customMessages);

我需要在选择字段下面显示错误消息…我的代码如下

 <div class="col-md-3">
  <select class="custom-select form-control" id="designation_id" value="{{old('designation_id')}}" name="designation_id">
     <option value="" class="disabled">-- Select Designation --</option>
         @foreach ($designations as $designation)
            <option value="{{ $designation->id }}"
                 @if(old('designation_id')== $designation->id) selected="selected" 
                 @endif> {{ $designation->name }} 
            </option>
         @endforeach
  </select>
</div>

--选择名称--
@foreach($指定为$指定)
id)selected=“selected”
@endif>{{$designation->name}
@endforeach

是的!就是这样!我已经尝试加入他们(访客id为必填项,未加入),但从未想过要将他们分开。谢谢,Leo_Kelmendi
$rules = [
   'unit'=> 'required|min:2|max:2',
   'visitor_id'=> 'required|not_in:14'
]
 $customMessages = [
    'unit.required' => 'Unit cannot be blank and must have 2 char max',
    'visitor_id.required' => 'Please choose from the list...'
    ];
$this->validate($request, $rules, $customMessages);
$rules = [
   'unit'=> 'required|min:2|max:2',
   'visitor_id'=> 'required|not_in:14'
]

 $customMessages = [
    'unit.required' => 'Unit cannot be blank and must have 2 char max',
    'visitor_id.required' => 'Please choose from the list...'
    'visitor_id.not_in' => 'Please choose from the list...' 
    ];

$this->validate($request, $rules, $customMessages);
 <div class="col-md-3">
  <select class="custom-select form-control" id="designation_id" value="{{old('designation_id')}}" name="designation_id">
     <option value="" class="disabled">-- Select Designation --</option>
         @foreach ($designations as $designation)
            <option value="{{ $designation->id }}"
                 @if(old('designation_id')== $designation->id) selected="selected" 
                 @endif> {{ $designation->name }} 
            </option>
         @endforeach
  </select>
</div>