Complex Validations

A couple of days ago, Michael Ramirez “asked me”: how to do complex validations on forms. He asked how one could have a validation as follows: Given 3 text fields A, B and C and a check box E, A is mandatory only if B and C are filled or if E is checked. I thought this would make a great little sample of both complex validation scripts and what I like to call the “Two Button Submit” technique.

“Download Sample [pdf]“:

*Minimum Requirements:* Designer 7.x, Acrobat 7.x.

h2. The PlanFirst, let’s figure-out what the plan is before we start scripting. The idea is to place these fields on a new form along with a submit button such that the user cannot submit the form unless the validation rules succeed. Since Field A isn’t always mandatory, we can’t default it to be _User Entered — Required_ by using the Value tab in the Object palette. We’ll need a way to make it required only when Fields B & C are filled or when Field E is checked. Furthermore, we don’t want the form’s data to be submitted unless these rules are met.The easiest solution that comes to mind is one that’s passive as the user is entering data into the fields but aggressive at the point when they try to submit it by putting the validation script on the submit button’s Click event. The catch here is that there’s no way of stopping the submission process once the Click event has fired so regardless of whether your validation succeeds or fails, the form is going to be submitted. That’s where the “Two Button Submit” technique comes-in to save the day.h2. “Two Button Submit” TechniqueThankfully, there’s a scripting method called _execEvent_ that lets you programmatically cause an event to be executed on a field. With this in mind, you place a regular (non-submit type) button on the form, make it visible and set its caption to read something like, “Submit”. Then you place the _actual_ submit button (could be email, HTTP or even print) on the form and make it _invisible_. Please note: make it *invisible*, not hidden or else the _execEvent_ call on it will fail because Acrobat won’t know it exists in the XFA Scripting Model. For this example, let’s call it “EmailSubmitButton”.Now that both buttons are on the form, in the regular (fake submit) button’s Click event, put the following script (FormCalc or JavaScript works just the same):bc. EmailSubmitButton.execEvent(“click”);With that script, when you click on the fake submit button, you’ll cause the Click event on the actual submit button to fire and the fake submit button will, to the user, behave just like a normal submit button.The advantage of this is that you now control if and when the real submit button’s Click event is executed and therefore if and when the form is submitted.If you were to add the following JavaScript statement before the _execEvent_ line:bc. if (FieldA.rawValue != null && FieldA.rawValue.length > 0)the form wouldn’t submit unless Field A was filled.h2. Validations Prior to SubmittingUsing the “Two Button Submit” technique, we can now perform any kind of validations — really simple to very complex — we want and have total control over if and when the form may be submitted.Given our example of fields A, B, C and E, we can do something like this:bc.. var bCanSubmit = true;if ( (IsSpecified(FieldB) && IsSpecified(FieldC)) ||IsChecked(FieldE) ){if (!IsSpecified(FieldA)){“Please fill Field A.”);bCanSubmit = false;}}if (bCanSubmit)EmailSubmitButton1.execEvent(“click”); // submit the formp. This will prevent the form from being submitted if Field A isn’t filled when Fields B & C are filled or Check Box E is checked.