问题
How to validate if user has picked at least one checkbox by validating on the server side in Laravel 5.2
I have a simple select box:
<label for="has_driverslicense">KFZ Führerschein: </label>
<select class="form-control" required="required" id="has_driverslicense" name="has_driverslicense">
<option value="0">Nein</option>
<option value="1">Ja</option>
</select>
and a hidden div:
<div id="kfz" style="display: none;">
<div>
<input id="checkbox-20" class="checkbox-style" name="B" type="checkbox">
<label for="checkbox-20" class="checkbox-style-3-label">B (Auto bis 3,49t)</label>
</div>
<div>
<input id="checkbox-21" class="checkbox-style" name="B+E" type="checkbox">
<label for="checkbox-21" class="checkbox-style-3-label">BE (Auto mit Anhänger)</label>
</div>
<div>
<input id="checkbox-22" class="checkbox-style" name="C1" type="checkbox">
<label for="checkbox-22" class="checkbox-style-3-label">C1 (LKW bis 7,49t)</label>
</div>
<div>
<input id="checkbox-23" class="checkbox-style" name="C" type="checkbox">
<label for="checkbox-23" class="checkbox-style-3-label">C (LKW bis 40t)</label>
</div>
<div>
<input id="checkbox-24" class="checkbox-style" name="CE" type="checkbox">
<label for="checkbox-24" class="checkbox-style-3-label">CE (LKW mit Anhänger)</label>
</div>
<div>
<input id="checkbox-25" class="checkbox-style" name="fahrerkarte" type="checkbox">
<label for="checkbox-25" class="checkbox-style-3-label">Fahrerkarte</label>
</div>
<div>
<input id="checkbox-26" class="checkbox-style" name="gabelstapler" type="checkbox">
<label for="checkbox-26" class="checkbox-style-3-label">Gabelstaplerführerschein</label>
</div>
<div>
<input id="checkbox-27" class="checkbox-style" name="gelaendestapler" type="checkbox">
<label for="checkbox-27" class="checkbox-style-3-label">Geländestaplerführerschein</label>
</div>
<div>
<input id="checkbox-28" class="checkbox-style" name="IPAF" type="checkbox">
<label for="checkbox-28" class="checkbox-style-3-label">IPAF – Arbeitsbühnen</label>
</div>
</div>
When user chooses yes/ja he should choose which category does he have. At the moment all works but i can get a user with driving licence but no category and that is baaaad! :) If you know what i mean! Help please.
Here is the FIDDLE example
回答1:
You need to name all of your checkboxes with the same name attribute, ie name="license_type" and you can then switch their names to value ie value="B".
Once this is done in your validation rules you would set.
'license_type' => 'required_if:has_driverslicense,1'
来源:https://stackoverflow.com/questions/38614494/force-user-to-pick-at-least-one-checkbox-laravel