Make hidden field mandatory when visible

Hi

I am very new to creating Forms and have been thrown in the deep end.

I have a form that I have set up using some JavaScript found around this site to achieve several things (show/hide fields based on drop down value etc.) and there is one thing that I need.

If I have some fields hidden by default unless a particular option is selected in a dropdown, I need those fields to become mandatory when visible and prevent submission unless completed (there are quite a few fields, essentially a mini-form within the main form.)

The thing is that I already have some validation script on the dropdown box which shows/hides fields (the ones that will be mandatory once visible):

"if (event.value == "16-18") {
this.getField("Grading1").display = display.visible;
} else {
this.getField("Grading1").display = display.hidden; }"

What I need is to have these fields become mandatory when selected. I have found the following snippet:

"this.getField("Grading1").display = display.visible ;
this.getField("Grading1").required = true ;"
and
"this.getField("Grading1").display = display.hidden ;
this.getField("Grading1").required = false ;"

When I add this to the validation section it does highlight the field as mandatory but still lets me submit the form even if nothing is entered (the target is a dropdown box that needs an option selecting.)

My question is:
Where do I add the code snippet? Is it in the validation script on the dropdown box which I have the other code in, or in the submit button (which runs some other validation JavaScript which creates an email with a subject based on fields in the form, and makes some fields mandatory.)

Hope this makes sense. Any help is much appreciated.


Darren Richards


Voted Best Answer

It's not about where to add the code. You are already doing the right thing if you see that the form fields get the red indicator that they are mandatory.

The problem is the meaning of "mandatory". The mandatory property is very limited in it's use: You can only use it for text fields, and, the state is only honored when you actually submit the form via a real forms submission. I assume you are just using a Doc.mailDoc() or mailForm() to email the filled out form back to you. That will ignore the mandatory flags. Does that mean they are useless? No, but you have to do the additional work of finding out if all your mandatory fields are filled in. There are plenty of examples on this site that show how to do that in a loop. Here is one such example: https://answers.acrobatusers.com/Loop-Through-Required-Fields-Submitting-Form-q277024.aspx

Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com


By Karl Heinz Kremer   


Please specify a reason: