How to make a group of checkboxes mandatory

dianeramos
Tera Contributor

I have a group of checkboxes i want to set as mandatory from the Form Design. I know from catalog item, adding a label and setting as mandatory will do the trick. Can I please get suggestions on how I can do this from a Form Design view?

Thank you

 

1 ACCEPTED SOLUTION

I've only ever used this as a Catalog Client Script rather than a Client Script; but that shouldn't really make a huge difference, other than where/when it runs.

View solution in original post

20 REPLIES 20

-O-
Kilo Patron
Kilo Patron

I would make those a glide list in which users can select any number of options and it can be governed by UI Policies and Data Policies. Also saves on table fields, etc.

Ian Mildon
Tera Guru

You could use something similar to this onSubmit script I used for requiring checkbox selections for a catalog item (record producer):

function onSubmit(){
    //Set the mandatory checkbox variable names and total mandatory count here
    var mandatoryVars = 'md,do,pa,pa_c,pt,pharm_d,aprn_cp,aprn_cnm,rn,lpn,ma,phd,lpc,msw,lcsw,ld,crna,mha';
    var mandatoryCount = 1;
   
    var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
    if(!passed){
        //Abort the submit
        alert('You must select at least ' + mandatoryCount + ' options.');
        return false;
    }
}

function forceMandatoryCheckboxes(mandatory, count){
    //Split the mandatory variable names into an array
    mandatory = mandatory.split(',');
    var answer = false;
    var varFound = false;
    var numTrue = 0;
    //Check each variable in the array
    for(x=0;x<mandatory.length;x++){
        //Check to see if variable exists
        if(g_form.getControl(mandatory[x])){
            varFound = true;
            //Check to see if variable is set to 'true'
            if(g_form.getValue(mandatory[x]) == 'true'){
                numTrue ++;
                //Exit the loop if we have reached required number of 'true'
                if(numTrue >= count){
                    answer = true;
                    break;
                }
            }
        }
    }
    //If we didn't find any of the variables allow the submit
    if(varFound == false){
        answer = true;
    }
    //Return true or false
    return answer;
}

In this example I was only enforcing a single checkbox (out of the multiple ones on the form) but by adjusting the "mandatoryCount" line will enforce any number you want. Just insure you have entered their values in the "mandatoryVars" line so the script knows which ones to check/enforce.

Thank you so much! It works like a charm…

I have a follow up question, I hope you can help…

We only want this client script to run if the Issue Type is Bug.

Where can I squeeze in that line in the code?

 

Appreciate your help!

Probably the best option would be on the "if" statement; something like this:

function onSubmit(){
    //Set the mandatory checkbox variable names and total mandatory count here
    var mandatoryVars = 'md,do,pa,pa_c,pt,pharm_d,aprn_cp,aprn_cnm,rn,lpn,ma,phd,lpc,msw,lcsw,ld,crna,mha';
    var mandatoryCount = 1;
   
    var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
    var getType = g_form.getValue('<issue type field>'); //is it a "bug"?

    if(!passed && getType == 'bug'){
        //Abort the submit
        alert('You must select at least ' + mandatoryCount + ' options.');
        return false;
    }
}

function forceMandatoryCheckboxes(mandatory, count){
    //Split the mandatory variable names into an array
    mandatory = mandatory.split(',');
    var answer = false;
    var varFound = false;
    var numTrue = 0;
    //Check each variable in the array
    for(x=0;x<mandatory.length;x++){
        //Check to see if variable exists
        if(g_form.getControl(mandatory[x])){
            varFound = true;
            //Check to see if variable is set to 'true'
            if(g_form.getValue(mandatory[x]) == 'true'){
                numTrue ++;
                //Exit the loop if we have reached required number of 'true'
                if(numTrue >= count){
                    answer = true;
                    break;
                }
            }
        }
    }
    //If we didn't find any of the variables allow the submit
    if(varFound == false){
        answer = true;
    }
    //Return true or false
    return answer;
}

dianeramos
Tera Contributor

I think i broke it 😞

I added this:

function onSubmit(){
//Set the mandatory checkbox variable names and total mandatory count here
var mandatoryVars = 'u_all_lines_of_busines,u_commercial_auto,u_commercial_farm,u_commercial_property,u_fleet,u_farm_package,u_personal_auto,u_personal_property,u_umbrella';
var mandatoryCount = 1;

var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
var getType = g_form.getValue('u_issue_type'); //is it a "bug"?

if(!passed && getType == 'bug'){
//Abort the submit
alert('You must select at least ' + mandatoryCount + ' options.');
return false;

}

 

Now it allows submission without selecting 1 from the checkboxes 😞