I am trying to create a Checkbox List using Knockout and require that at least one Checkbox is checked using Knockout Validation.
The issue that I am having is that the required validation message appears multiple times, once for each option, if no checkbox is checked. How do I resolve this issue?
working example: http://jsfiddle.net/aaronhoffman/BtK3t/
The HTML
<script id="koValidationCustomMessageTemplate" type="text/html">
<em class="text-danger" data-bind="validationMessage: field"></em>
</script>
<div class="col-sm-5" data-bind="foreach: TheCheckboxListOptions">
<div class="checkbox">
<label>
<input type="checkbox" data-bind="value: $data, checked: $root.TheCheckboxListSelectedValues" /><span data-bind="text: $data"></span>
</label>
</div>
</div>
and the javascript
ko.validation.configure({
registerExtenders: true,
messagesOnModified: true,
insertMessages: true,
parseInputAttributes: true,
messageTemplate: "koValidationCustomMessageTemplate"
});
var TheViewModel = {
TheCheckboxListOptions: [
"Option 1",
"Option 2",
"Option 3",
"Option 4"
],
TheCheckboxListSelectedValues: ko.observableArray().extend({
required: { message: "At least one option is required." }
}),
};
ko.applyBindings(TheViewModel);