Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Group separate form checkboxes into single field with required status

Community Beginner ,
Feb 17, 2020 Feb 17, 2020

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

TOPICS
PDF forms
2.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
2 ACCEPTED SOLUTIONS
Community Beginner ,
May 21, 2024 May 21, 2024

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? 

 

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 21, 2024 May 21, 2024

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. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 18, 2020 Feb 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 25, 2020 Feb 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 25, 2020 Feb 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 02, 2020 Mar 02, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 02, 2020 Mar 02, 2020

Luke, you are correct that there is an extra parentheses at the end of both lines, here is the correct code

 

var bOneSelected = this.getField("Check").getArray().some(function(a){return a.value!="Off"});
this.getField("Check").getArray().forEach(function(a){ a.required=!bOneSelected;});

 

I tested the code and it works fine

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 21, 2024 May 21, 2024

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? 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 21, 2024 May 21, 2024

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. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 22, 2024 May 22, 2024

Hi Thom. 

Thank you responding. 

I did change the name of the second set of checkboxes to "Group2". The first set was "Group1". 

 

Below is the code I used with the second set.

var bOneSelected = this.getField("Group2").getArray().some(function(a){return a.value!="Off"});
this.getField("Group2").getArray().forEach(function(a){ a.required=!bOneSelected;});

 

I am having the same issues the orginal poster had with the REQUIRED status of each checkbox remaining inactive. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 22, 2024 May 22, 2024

Try this correction:

 

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

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 22, 2024 May 22, 2024

I'm sorry, but that still didn't work. 

Would you happen to have any other suggestions? 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 22, 2024 May 22, 2024

Did you place this code into a custom calculation script on a hidden field? 

That is the intention of the script.

It makes the checkbox group required when no checkboxes are checked.  And it works.

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 22, 2024 May 22, 2024

Yes. 

Here is a screenshot of what I am trying to do. 

Jasidy37537824lq83_0-1716404586191.png

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 22, 2024 May 22, 2024

Are any messages reported in the console window? Are any other scripts affecting these checkboxes?

 

Something is not working, so you need to do some debug. Add this line to the script.  Open the console window and watchg it while clicking the checkboxes on and off. 

 

 

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

console.println("One Selected:" + bOneSelected + "  Required:" + this.getField("Group2").required);

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 23, 2024 May 23, 2024
LATEST

Hi Thom. 

I did the debugging and it showed that the text in this.getField = null. 

I had to remove the "" in the field name from each box to get the code to find the group and activate the requirement. 

Thank you so much for your help. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines