如何 return 在 Laravel 验证中验证 ID 的值
How to return validated value of ID in Laravel validation
所以我创建了循环生成的单选按钮:
<div class="custom-control custom-radio guest-form">
@foreach(config('const.res_site') as $id => $res)
<div class="form-radio">
<input class="custom-control-input" type="radio" onchange="otherResSite({{$id}})" id="{{$res}}" value="{{$id}}"
name="reservation_site" {{ old("reservation_site") == $id ? "checked" : "" }}>
<label for="{{ $res }}" class="custom-control-label">{{ $res }}</label>
</div>
@endforeach
<div class="otherField" id="ifOtherSite" class="otherSite" style="display: {{ old('reservation_site') == 4 ? 'display:inline-flex' : 'none' }}">
<input type='text' class="form-control" id='otherSite' name='otherSite' value="{{ old('otherSite', '') }}"><br>
</div>
</div>
@if ($errors->has('otherSite'))
<div class="form-group">
<p class="text-danger">{{ $errors->first('otherSite') }}</p>
</div>
@endif
const.php
'res_site' => [
0 => 'site1',
1 => 'site2',
2 => 'other',
],
如果所选选项是其他选项,则此选项用于验证 otherSite
值。它现在运行良好,但验证消息 returned 是这样的:
The other site field is required when reservation site is 2.
我的验证器是这样的:
return [
'reservation_site' => ['required'],
'otherSite' => ['required_if:reservation_site,2'],
]
现在,我怎样才能使它的 return 消息成为
The other site field is required when reservation site is other
有什么办法可以做到吗?
好的。如果有一天有人需要这个,我是这样做的:
<input class="custom-control-input" type="radio" id="{{$res}}" value='{{$id == 4 ? "other" : $id}}'>
在我的验证中:
return [
'reservation_site' => ['required'],
'otherSite' => ['required_if:reservation_site,other'],
]
所以我创建了循环生成的单选按钮:
<div class="custom-control custom-radio guest-form">
@foreach(config('const.res_site') as $id => $res)
<div class="form-radio">
<input class="custom-control-input" type="radio" onchange="otherResSite({{$id}})" id="{{$res}}" value="{{$id}}"
name="reservation_site" {{ old("reservation_site") == $id ? "checked" : "" }}>
<label for="{{ $res }}" class="custom-control-label">{{ $res }}</label>
</div>
@endforeach
<div class="otherField" id="ifOtherSite" class="otherSite" style="display: {{ old('reservation_site') == 4 ? 'display:inline-flex' : 'none' }}">
<input type='text' class="form-control" id='otherSite' name='otherSite' value="{{ old('otherSite', '') }}"><br>
</div>
</div>
@if ($errors->has('otherSite'))
<div class="form-group">
<p class="text-danger">{{ $errors->first('otherSite') }}</p>
</div>
@endif
const.php
'res_site' => [
0 => 'site1',
1 => 'site2',
2 => 'other',
],
如果所选选项是其他选项,则此选项用于验证 otherSite
值。它现在运行良好,但验证消息 returned 是这样的:
The other site field is required when reservation site is 2.
我的验证器是这样的:
return [
'reservation_site' => ['required'],
'otherSite' => ['required_if:reservation_site,2'],
]
现在,我怎样才能使它的 return 消息成为
The other site field is required when reservation site is other
有什么办法可以做到吗?
好的。如果有一天有人需要这个,我是这样做的:
<input class="custom-control-input" type="radio" id="{{$res}}" value='{{$id == 4 ? "other" : $id}}'>
在我的验证中:
return [
'reservation_site' => ['required'],
'otherSite' => ['required_if:reservation_site,other'],
]