我有一张有复选框列表的表格。最后一个显示“其他”,单击时,将启用输入文本。
如您所知,复选框存储在一个数组中。
这里是
options_list.blade.php
查看:
@section('content')
@if($errors->any())
<div class="alert alert-danger" role="alert">
<strong><i class="fas fa-exclamation-triangle"></i> Warning</strong>: The following errors have been found:
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<div class="card">
<div class="card-body">
<div class="shadow p-3 mb-5 bg-white rounded">{{--Referencias: https://getbootstrap.com/docs/4.1/utilities/shadows/--}}
<p class="h6">
Here goes the question text
</p>
<p class="text-primary">You can choose up to three options</p>
</div>
<div class="shadow">
<form action="{{ route('survey1.post',$token) }}" method="post" id="myForm">
<div class="col-lg">
@foreach($lineasdeinvestigacion as $lineadeinvestigacion)
<div class="custom-control custom-checkbox my-1 mr-sm-2">
<input type="checkbox" class="custom-control-input" id="customControlInline{{ $loop->index + 1 }}" name="lineasdeinvestigacion[]" value="{{ $lineadeinvestigacion->linea }}" {{ old('lineasdeinvestigacion') && in_array($lineadeinvestigacion->linea,old('lineasdeinvestigacion')) ? 'checked' : (isset($encuesta) && ($encuesta->fortalecer_linea_1 == $lineadeinvestigacion->linea || $encuesta->fortalecer_linea_2 == $lineadeinvestigacion->linea || $encuesta->fortalecer_linea_3 == $lineadeinvestigacion->linea)) ? 'checked' : '' }}>
<label class="custom-control-label" for="customControlInline{{ $loop->index + 1 }}">{{ $lineadeinvestigacion->linea }}</label>
</div>
@endforeach
<div class="custom-control custom-checkbox my-1 mr-sm-2">
<input type="checkbox" class="custom-control-input" id="customControlInlineOtro" name="lineasdeinvestigacion[]" value="other" {{ old('lineasdeinvestigacion') && in_array('other',old('lineasdeinvestigacion')) ? 'checked' : (isset($encuesta) && ($encuesta->fortalecer_linea_1 == 'other' || $encuesta->fortalecer_linea_2 == 'other' || $encuesta->fortalecer_linea_3 == 'other')) ? 'checked' : '' }}>
<label class="custom-control-label" for="customControlInlineOtro">Other</label>
<input placeholder="" type="text" class="form-control form-control-sm" id="fortalecer_otro" name="fortalecer_otro" maxlength="255" value="{{ old('fortalecer_otro') ? old('fortalecer_otro') : '' }}" disabled>
</div>
@include('path.to.partials.buttons._continue'){{-- includes @csrf --}}
</div>
</form>
</div>
</div>
</div>
@endsection
这里是
optionsController.php
:
public function store(Token $token, Request $request){
//dd($request->lineasdeinvestigacion);
//Validating input data
$this->validate($request,[
'lineasdeinvestigacion' => 'nullable|max:3',
'fortalecer_otro' => 'required_if:lineasdeinvestigacion.*,other|max:255',
],[
'lineasdeinvestigacion.max' => 'You cannot choose more than :max options.',
]);
}
dd($request->lineasdeinvestigacion);
):
array:4 [â¼
0 => "Procesos socio-culturales"
1 => "Ciencia, Innovación tecnológica y Educación"
2 => "Nuevas formas de movilidad"
3 => "other"
]
#fortalecer_otro
要空,当“他者”
#customControlInlineOtro
选中复选框选项。
解决方法
我认为一种解决方法是分离数组的最后一项,因为要验证的输入,如果
最后的
项(复选框)具有
other
值,并将其添加为要验证的另一个元素,如中所述
this answer
.
this one
,它讨论验证最后一个。在我的例子中,我必须计算项目的数量,然后,指示验证数字x,这将是最后一个项目。。。
我该怎么解决这个问题?有什么想法吗?
我意识到了,多亏了
second link
count the number of items in an array
,然后应用
required_if
if($request->lineasdeinvestigacion){
$otro_item = count($request->lineasdeinvestigacion) - 1;
echo '<p>"other" is the item: '.$otro_item.'</p>';
}else{
echo '<p>Nothing was selected in the checkboxes list</p>';
}
//dd($request->lineasdeinvestigacion);
//Validating input data
$this->validate($request,[
'lineasdeinvestigacion' => 'nullable|max:3',
'fortalecer_otro' => 'required_if:lineasdeinvestigacion.'.$otro_item.',otro|max:255',
],[
'lineasdeinvestigacion.max' => 'No puede elegir más de :max opciones.',
]);
这就成功了。