Reputation: 10720
I got a form with a list of checkboxes. The last one says "other", when clicked, an input text is enabled.
I have this rule where the user can check up to three options.
As you already know, checkboxes are stored in an array.
Should the user check on "other" option, without typing in the input, I want to prompt the user through an error message (validation) that they need to type in the input text, as well.
Here is options_list.blade.php
view:
@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
And here is the 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.',
]);
}
This is the array of values chosen from the checkboxes list (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"
]
However, the validation is not working as it should, as it allows the input text #fortalecer_otro
to be empty, when the "other" #customControlInlineOtro
checkbox option is checked.
Way to solution
I think one workaround would be to separate the last item of the array, since the input to validate, if the last item (checkbox) has the other
value, and add it as another element to be validated, as stated in this answer.
Or in this one, it talks about validating the last one. In my case, I would have to count the number of items and then, indicate to validate the number x, which would be the last item ...
How do I fix this? Any ideas?
Solved
I have realized, thanks to the second link that I should count the number of items in an array and then indicate inside the validation rules, which item check if value is other
, then apply the 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.',
]);
And that did the trick .
Upvotes: 0
Views: 1887
Reputation: 10720
I have realized, thanks to the second link that I should count the number of items in an array and then indicate inside the validation rules, which item check if value is other
, then apply the 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.',
]);
And that did the trick .
Upvotes: 0