Force user to pick at least one checkbox - Laravel

与世无争的帅哥 提交于 2020-04-11 04:43:08

问题


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&uuml;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&auml;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&auml;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&uuml;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&auml;ndestaplerf&uuml;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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!