Search code examples
validationcheckboxdojospring-js

Multiple CheckBox Validation using spring JS?


I am creating dynamic Check Boxes in runtime. my requirement is have to validate atleast one checkbox checked or not. am doing this using springJS. But to validate i have to pass checkBox Id to spring Validation, But this ID array creating in runtime. how can i achieve this? i tried all solutions but it did'nt worked for me. i am doing like this it was working if i hardcode checkbox id.

<script type="text/javascript">
    Spring.addDecoration(
        new Spring.ElementDecoration({
            elementId: '_CheckBox_ids',
            widgetType: 'dijit.form.CheckBox',
            widgetModule: 'dijit.form.CheckBox',
            validate: function () {
                if (dojo.query("#roo_apiUser_profile > input[type=checkbox]", 'dijit.form').filter(function (n) {
                    return n.checked;
                }).length > 0) {
                    return true;
                } else {
                    alert('choose at least one profile');
                    return false;
                }
            },
            widgetAttrs: {
                required: true
            }
        }));
</script>

Solution

  • You may add the onsubmit attribute in your form tag and call a function that does the validation on checkboxes.

    Alternately, you may create your own extension of AbstractElementDecoration (defined in spring.js) using dojo.declare and pass to the validate attribute a function that will do the checkboxes validation for you. For elementId you may pass in the id of a div element that contains all the checkboxes. Spring.ValidateAllDecoration calls Spring.validateAll function. Make sure you do the necessary tweaking in your AbstractElementDecoration extension so that there is no exceptions in Spring.validateAll.