Process All Fields

A common requirement on the “Adobe Designer Forums”:http://www.adobeforums.com/cgi-bin/webx/.3bb7d189/ is to find all fields on a form and do something specific with them.

For instance, you may want all mandatory fields to be automatically highlighted when a user attempts to submit a form electronically before having filled all mandatory fields. While Acrobat provides a button to toggle mandatory field highlighting on/off (via the _Highlight required fields_ check box in the yellow Form Field toolbar), Acrobat’s Scripting Object Model doesn’t provide a function to do the same. Therefore, you’re left having to write some script to achieve the same functionality.

Since this is requested so often, I thought I would try to put together some canned script that you can copy and paste into your form in order to instantly have the ability to make changes on everything that’s considered a field on your form.

“Download Script [js]”:http://blogs.adobe.com/formbuilder/samples/ProcessAllFields.js
“Download Sample [zip]”:http://blogs.adobe.com/formbuilder/samples/ProcessAllFields.zip

*Minimum Requirements:* Designer 7.0, Acrobat 7.0.


The first download is for a JavaScript file which contains the “plug & play” script that you can simply copy & paste into any event handler or script object and immediately start concentrating on what you want to do when you find a button, a check box, a text edit, etc.It’s divided into two parts: The first (at the top-end of the script) is a series of _Field Processor Functions_ for each field/object type that may be found within a form. Each function receives a reference to the field in question. The second (at the bottom-end of the script) defines the _ProcessAllFields_ function — the brains behind this operation. This function detects the field type and calls the appropriate _Field Processor Function_.All you need to do is add script to the _Field Processor Function(s)_ for the field types you need to do stuff with.The second download contains the JavaScript file along with a sample form which demonstrates how to get from the bare-bones _Field Processor Functions_ and _ProcessAllFields_ function to a solution which finds all non-filled mandatory fields on a form and highlights them with the current Acrobat Highlight Color. You’ll find customized versions of the _Field Processor Functions_ and _ProcessAllFields_ function in the script object (named “ScriptObject” — yes, I know, this is an unbelievably original name!) while the script execution flow begins in the Submit button’s Click event.When the Submit button is clicked, the form is search for non-filled (empty) mandatory fields. If such fields are found, they’ll be highlighted, the RemoveHighlight button will be made visible and an error message will be displayed. Otherwise, the Email Submit dialog will open.

7 Responses to Process All Fields

  1. Hi Stefan,It occurs to me that it would be nice to remove the recursion in this example. My thinking was that I should be able to utilize xfa.resolveNodes() and the proper SOM expression to get a nodelist of all the fields. I haven’t been able to concoct the right SOM expression however. I thought “xfa.form..#field[*]” should work after all:The .. notation indicates a recursive search through the children of an object.The # notation allows you to access a note by type.The [*] notation returns all the occurrences that match.Unfortunately, this doesn’t work. Any idea why?

  2. OK, I just re-examined the documentation for the .. operator. While the docs don’t say it, the examples indicate that the .. operator only finds the first occurrence of a subcontainer below the parent. That seems a little odd to me. Is there some way to find all occurrences within all subcontainers?

  3. Rob,It took me a little while to figure-out what was going on with the FormCalc double-dot (“..”) operator.While documentation on the subject may lead one to think that it’s a recursive operator or that recursion is possible with it, the operator isn’t designed to be recursive.The double-dot “..” operator is specific to FormCalc and is designed to perform a top-to-bottom, depth-first search of the RHS (right hand side) operand looking for a single object which matches the RHS. Once that object is found, if the RHS is augmented with the “[*]” syntax to locate all occurrences of an object, it looks for all occurrences only within the container which was found to contain the first match.Consider the following simplified object hierarchy:R+– A+– C+– C+– B+– C+– CSearching for “R..C” would return the first C from the top which is parented to A.Searching for “R..C[*]” would return the first two Cs from the top, both parented to A, but none of the Cs parented to B.

  4. Steve Lodge says:

    Stefan,I am trying customize your example here..I am creatig exams where the push of one button will look for the correct answer (radio button in an exclusion group with a value of 1) of every multiple choice question and will add each correct answer to a sum calculation for the final mark. If the user selects an incorrect answer, the incorrect answer is highlighted and the correct answer is selected. I figured your example would be perfect to manipulate for this but don’t want to break what you’ve created being an intermediate developer. I was hoping you could give me some insight.Hope you can help.Steve

  5. Steve,I can see why you would want to use my sample as a base to work with.I would suggest that you first modify the “ProcessAllFields” function in the “ScriptObject” script object such that it calls a new function named “ProcessExclusionGroup” for every exclusion group that it finds as it recursively iterates through the XFA Object Model. At the moment, the “ProcessAllFields” function will call the “ProcessRadioButton” function for every radio button within an exclusion group which means you’ll end-up verifying the same exclusion group many times.Once you have the “ProcessExclusionGroup” function in place, then you can loop through the nodes it contains to find the radio buttons in the same way that the “ProcessAllFields” function does, simply looking for nodes with a “field” class name (since you know they’ll be radio buttons at this point).Also, given the exclusion group, you can get its rawValue property which will tell you the value of the selected radio button. Since the correct answer has a radio button with a value of 1, if the exclusion group’s rawValue is 0, you know the student didn’t select the correct answer. You can then loop through the radio buttons, highlighting the incorrect selection in red and setting the exclusion group’s rawValue property to 1 (which will automatically select the radio button with the correct answer).Please let me know if you have any questions.

  6. Newbie says:

    I am a newbie to Livecycle Designer 7.0 – I just created a survey with multiple required fields. I like the fact that before a user can submit the form via email, required fields are highlighted so the user knows they are required and cannot submit until filled-in. What I was wondering, is there a way to make ONLY the required fields THAT WERE MISSED highlighted so they can be found easier? It seems so cluttered when a required field, that is already filled in, becomes highlighted in addition to those fields that are not filled in. Thanks

  7. Newbie,Based on your question, it sounds like you’ve already figured-out how to highlight all the required fields when the user clicks the Submit button. Now you’re just wanting the empty required fields to be highlighted instead of all of them, correct?In that case, you should be able to tweak your script to ignore fields which have a value. You can determine if a field has a value by performing the following test on a field object reference (“MyField”):

    // JavaScript:if (MyField.rawValue =! null && MyField.rawValue.length > 0)    // the field has a value// FormCalc:if (HasValue(MyField)) then    // the field has a valueendif