Skip to main content
MarlowMarketing
Participant
February 17, 2020
Answered

Group separate form checkboxes into single field with required status

  • February 17, 2020
  • 2 replies
  • 3105 views

Hi, I am putting together a form which includes several mutiple-choice sections where you can select one or more options.

 

The out-of-box listboxes are not visually practical for my purposes, so I need to somehow get the form to recognise the grouped relationship between several checkboxes that have been arranged together to form the multiple choice answer options on the form, so that the question itself can be made a REQUIRED field, but only one or more of the checkboxes need to be selected to satisfy the REQUIRED criteria for that question. 

 

At present, the form considers all the checkboxes to be separate and independent, so the built-in REQUIRED functionality can't be applied correctly, as applied to the checkboxes in their separate state it would force the user to select every checkbox.

 

Does anyone know how to approach this problem (probably using Javascript) to enable the form to be filled in correctly?  Hope this question makes sense! 🙂

 

Thanks,

 

Luke

This topic has been closed for replies.
Correct answer Thom Parker

Hi Thom. 

I got this code to work on one group of check boxes. But can not get it to work on the second group. 

 

Can you help? 

 


To use this code on a set of checkboxes you'll need to change the field name used in the "getField" fucntions. There are two of them, one on each line. 

 

2 replies

Thom Parker
Community Expert
Community Expert
February 25, 2020

You definately need a script for this. First, group the fields using the "." notation in the field name. This will make them easier to handle as a group. For exmple, "Group1.Check1" "Group1.Check2", etc. 

 

Next, use the calculation script on a hidden text field to run the script that will test and set the required value.

 

// Test for at least one selected checkbox

var bOneSelected = this.getField("Group1").getArray().some(function(a){return  a.value!="Off"}))

// Set all to required if none are selected. None to requred if at least one is selected

this.getField("Group1").getArray().forEach(function(a){ a.required=!bOneSelected;}))

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
MarlowMarketing
Participant
March 2, 2020

Hi Thom,

 

It's very kind of you to offer some code to help me achieve my desired results.  I had a couple of problems with the code:

1. I needed to remove the extra end bracket off each line in order to make Adobe Acrobat accept the code and let me keep it in the Custom Calculation Script box (a syntax error message box was popping up when I tried to enter the code initially).

 

// Test for at least one selected checkbox

var bOneSelected = this.getField("Group1").getArray().some(function(a){return  a.value!="Off"}))

// Set all to required if none are selected. None to requred if at least one is selected

this.getField("Group1").getArray().forEach(function(a){ a.required=!bOneSelected;}))

 

2. I tested the form and even with my checkboxes renamed correctly and this code attached to a hidden text box, the REQUIRED status of each checkbox seemed to remain inactive, regardless of whether any of the boxes were selected.

 

I appreciate the effort you have gone to in trying to help me. 🙂

 

Luke

Thom Parker
Community Expert
Community Expert
February 18, 2020

Yes, this will require a script.  

The "required" field property only affects form submission. It means that a field is required to be something other than it's default value before Acrobat/Reader will allow a form submit action. 

 

What do you mean by "required"?  In the context of a scripted solution, it has to mean something that can be caught with a JavaScript event.

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
MarlowMarketing
Participant
February 25, 2020

Hi Thom, thanks for your reply.

 

The problem I have is that visually I want to use multiple individual checkboxes instead of the out-of-the-box dropdown list box to enable the user to select one or more options (they must select at least one option, which is why I need the required status).  Because these fields are not grouped together by default, the script would need to recognise when one of the checkboxes is ticked and disable the required status for the other checkboxes, or if all of them are unchecked again, it would enable the required status on these checkboxes again.

 

I don't know whether I could create a hidden field that holds a value drawn from all the checkboxes, that must contain a positive value from one of them before the submit button can be successfully pressed.

 

I'm not sure what the best scripting approach is, but it seems like the kind of functionality a lot of people out there would want to use.