Make Variable type 'Checkbox' mandatory

pstifter
Tera Contributor

I have created a label and added 4 Checkboxes which each must be read and checked prior to providing an electronic signature for a particular Human Resources catalog item request.   Even though the checkbox variables are marked as mandatory, one is not required to check these boxes in order to submit the request.

How does one make these variables each mandatory?

1 ACCEPTED SOLUTION

arnabwa
Giga Guru

Hi Paul,
I was in exactly the similar situation once a while. I used this code and it worked perfectly. Just paste the code & make variable name changes. Observe the magic.



Name:  Mandatory checkboxes
Type:  onSubmit
Applies to:  A Variable Set OR A Catalog Item
Script:



function  onSubmit(){
     //Set the mandatory checkbox variable names and total mandatory count here
     var  mandatoryVars  =  'option1,option2,option3,option4';
     var  mandatoryCount  =  2;
     
     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;
}


View solution in original post

19 REPLIES 19

ccajohnson
Kilo Sage

How are you making the variables mandatory?


-     Are you setting each one as mandatory when other conditions are met?


-     Are you setting all of the variables as mandatory without using Catalog UI Policies?



Please let us know what your design is so we can focus our solution accordingly.


arnabwa
Giga Guru

Hi Paul



Could not get your requirement clearly. Could you please elaborate on the matter? You say you have marked the check box variable mandatory but it is not necessary to check them for submission. This is very confusing (sorry to say). Please let us know your requirement with a bit more clarity.




Thanks,


Arnab


pstifter
Tera Contributor

Sorry, let me try to provide more detail.   First off I have not really worked with Checkboxes as independent variables, only as a multiple choice variable which allows you to make the question mandatory.   To help understand I'm trying to create a check list that requires all 4 statements be checked.   The intent is for the requester to read each statement and check it prior to signing the form electronically.  


Currently I have not created any UI policies or any conditions.   The conditions are all 4 independent checkbox variables must be checked in order for the catalog request may be submitted.



The checkbox variable allows one to make the question 'mandatory' within the default view, however, this does not force the question to be answered.



I may be going at this all the wrong way, so if a better solution makes sense.   Please provide.



Here is a print screen to help clarify.


find_real_file.png


Just make an onSubmit client script that checks to make sure each of the values for the checkbox variables is true.