Validate checkbox array and prefill with selected value on validation error in laravel 5

Say for example you have your checkbox field code in your twig file as below:-

@foreach ($serviceList as $servloop)
<div class=”checkbox”>
<label>
<input type=”checkbox” name=”services[]” value=”{{$servloop->service_id}}” {{ ( is_array(old(‘services’)) && in_array($servloop->service_id, old(‘services’)) ) ? ‘checked ‘ : ” }} />
{{$servloop->service_name}}
</label>
</div>
@endforeach

Controller file code

Validation Rule will be as below:-

$rules = [
‘services’ => ‘required|min:1’,  // min:1 means atleast one should be selected
];

$customMessages = [
‘services.required’ => ‘Select the service you will need from us, for and during the Expo.’,
];

 

 

 

Leave your comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.