javascriptyuiyui3alloy-ui

How do I validate a group of checkboxes with YUI3 AlloyUI?


I have a form field with a group of checkboxes and at least one of the many checkboxes must be selected in order to submit the form.

How do I use YUI3 rules to make this happen?

Many thanks, S

rules: {
    fname: {
        required: true,
    },
    email: {
        required: true,
        email: true
    },
    tel: {
        required: true,
        digits: true,
    },
    dob: {
        date: true,
    },
},
fieldContainer: '.form__item',
containerErrorClass: 'form__item--error',

HTML:

<fieldset class="form__item form__item--group">
    <legend class="form__item__label">
        A group of checkboxes
        <div class="form__item__label__instructions">
            Select one of them.
        </div>
    </legend>

    <input name='errorMessageAnchor' hidden/>

    <label class="form__item__label" for="cb1">
        <input id="cb1" name="cbName" type="checkbox" class="checkbox" /> One
    </label>
    <label class="form__item__label" for="cb2">
        <input id="cb2" name="cbName" type="checkbox" class="checkbox" /> Two
    </label>
    <label class="form__item__label" for="cb3">
        <input id="cb3" name="cbName" type="checkbox" class="checkbox" /> Three
    </label>
    <label class="form__item__label" for="cb4">
        <input id="cb4" name="cbName" type="checkbox" class="checkbox" /> Four
    </label>
</fieldset>

Solution

  • Looking at the source for aui-form-validator, the use of mix indicates how to approach a solution.

    For simplicity, I've also included a usage of gallery-checkboxgroups, specifically for CheckboxGroup to have access to allUnchecked function.

    HTML

    <form id="myForm">
     <div class="control-group">
    
        <div>
            <h5>Checkbox Group (requries at least 1 selection)</h5>
    
            Checkbox 1<input type='checkbox' class='checkbox' name='cbName'/><br/> 
            Checkbox 2<input type='checkbox' class='checkbox' name='cbName'/><br/>
            Checkbox 3<input type='checkbox' class='checkbox' name='cbName'/> 
        </div>
    </div>
    <input class="btn btn-info" type="button" value="Submit"/>
    

    JS

    YUI().use('aui-form-validator', 
              'gallery-checkboxgroups',
               function(Y) {
    
                 var group = new Y.CheckboxGroup(Y.all('.checkbox'))
    
                 var CONFIG = Y.config.FormValidator;
                 Y.mix(CONFIG.RULES, {atLeastOneCheckbox: function(val, fieldNode, ruleValue) {
                        return !group.allUnchecked()
                    }});
    
                 var validator = new Y.FormValidator(
                        {
                            boundingBox: '#myForm',
                            rules: {
                                cbName:{custom:true, atLeastOneCheckbox:true}
                            }
                        }
                );
    
            }
    );
    

    Fiddle


    To override the default error message

     Y.mix(CONFIG.STRINGS,{atLeastOneCheckbox:"Pick at least 1"});
    

    By default the form validator is going to validate onBlur and since all the checkboxes share the same name, the error message will move around respectively to the last "failed" validation field. To address this issue, place a hidden field <input name='errorMessageAnchor' hidden/> above the group, and associate the error with that field by replacing cbName in the rules

    errorMessageAnchor:{custom:true, atLeastOneCheckbox:true}