I'm trying to get ParsleyJS to work with a button group and drop down select. How can this be done?
I have tried adding it via JS but it's still not working. Here is my html code
<div class="btn-group m-r">
<button data-toggle="dropdown" class="btn btn-sm btn-white dropdown-toggle">
<span class="dropdown-label" data-placeholder='{{_ "pleaseSelect"}}' data-parsley-required>{{_ "type"}} </span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-select">
<li><a href="#"><input type="checkbox" name="d-s-c-1">Association</a></li>
<li><a href="#"><input type="checkbox" name="d-s-c-2">Cooperative</a></li>
<li><a href="#"><input type="checkbox" name="d-s-c-3">Cluster</a></li>
<li><a href="#"><input type="checkbox" name="d-s-c-4">Contract Farming</a></li>
<li><a href="#"><input type="checkbox" name="d-s-c-5">Individual Farm</a></li>
</ul>
</div>
You can't add
data-parsley-requiredto a span as it won't work. You need to add the attribute to the input.Assuming that at least one checkbox is required, you can use the following code (jsfiddle available):
What's changed:
data-parsley-requiredfrom<span>Added
data-parsley-multiple='d-s-c'to all checkboxes. Take a look at the documentation where it states:I'm assuming that you want to validate that, at least one checkbox is checked. If we didn't add this attribute, all checkboxes would be required.
requiredto the first checkbox. This makes the group defined bydata-parsley-multiplerequired.data-parsley-errors-container="#message-holder"so the error messages appear outside the dropdown. Note that I've added<div id="message-holder"></div>after the<ul>.As a final note, we only need to set 3. and 4. to the first checkbox. Since Parlsey will "group" the checkboxes you don't need to repeat this code for every checkbox (note: if you do repeat this code, there's no problem).