Loop Through Required Fields before Submitting Form

Hello,

I'm currently stuck on constructing my JS code to work the way I need it. Here's the scenario...

Scenario: End user fills out the form and forgets to enter
1 (or more) required fields. They then go to click
the "Submit" button and immediately they are prompted to
fill out only the required fields that were left blank.


Once they do that, if they hit the "Submit" button a 2nd
time, this time it should recognize the required fields
have a value in them and skip to the next empty required
field or skip to the next part of the function (which
already exists in the code), where it asks to "Click YES to
complete or NO to edit".


Q: How do I structure the code so end users are first
prompted to fill in the missing required fields?

The first required filed in my code is let's say "Field1".
The way my code is structured now, for some reason, even
after I fill in Field1 (without filling out the other
required fields), it still pops up a message saying Field1
needs to be filled out (which is incorrect).


I need the script to recognize if a required filed is no
longer empty and skip to the next empty required field (I'm
not checking for any non-required fields).


Here's my code:

-----BEGIN-----
// Email for "To" field
var cToAddr = ""
var cCCAddr = ""
var cBCCAddr = this.getField("BRANCH_DROPDOWN").value

var VanNum = this.getField("VAN_NUM").value;
var Date = this.getField("WORK_ORDER_DATE").value;
var invoiceTo = this.getField("INVOICE_TO").value;


// Flatten PDF (Read Only). This doen't work in PDF Expert, therefore it's disabled
// this.flattenPages();

// Set the subject and body text for the email message
var cSubLine = "Complete Service WO for " + invoiceTo + " -- " + Date;
var cBody = "Hello, \n \n" + "The Work Order opened " + Date + " for " + invoiceTo + " has been completed and attached for your records. \n \n" + "Please feel free to contact our service department with any questions, contact details have been provided below. \n \n" + "Thank you for your business! \n \n" + "Service Technician, Van#: " + VanNum + "\n" + "Southeast Industrial Equipment \n" + "12200 Steele Creek Rd, Charlotte NC 28273 \n" + "704-399-9700";

(function () {

var num=0;
for(var i = this.numFields - 1; i > -1; i--)
{
var fieldName = this.getNthFieldName(i);
if(this.getField(fieldName).required==true)
{
app.alert("Please complete field "+ fieldName);
//event.target.textColor = color.red;
num=1;
break;

if (num==0){

// Prompt the user with a Yes/No question
var resp = app.alert({cMsg: "Click 'YES' to complete\n" + "(Read-Only PDF)\n\n" + "Click 'NO' to edit", nIcon: 1, nType: 2, cTitle: "Submit Service WO Form?"});

// If the user clicked the "Yes" button...
if (resp === 4) {

// make all fields in a form read only;
var oField; // variable for field being processed;

// loop through the form fields;
for (var i = 0; i < this.numFields; i++) {

// process each field name;
oField = this.getField(this.getNthFieldName(i)).readonly = true;}

// Send the email
try {
mailDoc({
bUI: true,
cTo: cToAddr,
cCc: cCCAddr,
cBcc: cBCCAddr,
cSubject: cSubLine,
cMsg: cBody
});

}

catch(e) {
app.alert("Could not send email, sorry.", 1);
}
}
}


}
}
})();
-----END-----


J. Hall


Voted Best Answer

There are several problems with your code: The first one is that you are not actually checking the value of a required field: It does not matter what the field is set to, you are always displaying the message. The next problem is that you are not excluding buttons from your check - a button does not have a "required" property, so it will actually throw an exception and your script will stop. The next problem is that your submission routine will be called as soon as the first field that has a value is found, regardless of what the other fields are set to. Try this instead:

// Email for "To" field
var cToAddr = ""
var cCCAddr = ""
var cBCCAddr = this.getField("BRANCH_DROPDOWN").value
var VanNum = this.getField("VAN_NUM").value;
var Date = this.getField("WORK_ORDER_DATE").value;
var invoiceTo = this.getField("INVOICE_TO").value;

// Flatten PDF (Read Only). This doen't work in PDF Expert, therefore it's disabled
// this.flattenPages();

// Set the subject and body text for the email message
var cSubLine = "Complete Service WO for " + invoiceTo + " -- " + Date;
var cBody = "Hello, \n \n" + "The Work Order opened " + Date + " for " + invoiceTo + " has been completed and attached for your records. \n \n" + "Please feel free to contact our service department with any questions, contact details have been provided below. \n \n" + "Thank you for your business! \n \n" + "Service Technician, Van#: " + VanNum + "\n" + "Southeast Industrial Equipment \n" + "12200 Steele Creek Rd, Charlotte NC 28273 \n" + "704-399-9700";

(function() {

    var formIsComplete = true;
    for (var i = 0; i < this.numFields; i++) {
        var fieldName = this.getNthFieldName(i);
        var f = this.getField(fieldName);

        if (f != null && f.type != "button" && f.required == true && f.value == "") {
            app.alert("Please complete field " + fieldName);
            //event.target.textColor = color.red;
            formIsComplete = false;
            break;
        }
    }

    if (formIsComplete) {
        // Prompt the user with a Yes/No question
        var resp = app.alert({
            cMsg: "Click 'YES' to complete\n" + "(Read-Only PDF)\n\n" + "Click 'NO' to edit",
            nIcon: 1,
            nType: 2,
            cTitle: "Submit Service WO Form?"
        });

        // If the user clicked the "Yes" button...
        if (resp === 4) {

            // make all fields in a form read only;
            var oField; // variable for field being processed;

            // loop through the form fields;
            for (var i = 0; i < this.numFields; i++) {

                // process each field name;
                oField = this.getField(this.getNthFieldName(i)).readonly = true;
            }

            // Send the email
            try {
                mailDoc({
                    bUI: true,
                    cTo: cToAddr,
                    cCc: cCCAddr,
                    cBcc: cBCCAddr,
                    cSubject: cSubLine,
                    cMsg: cBody
                });

            } catch (e) {
                app.alert("Could not send email, sorry.", 1);
            }
        }
    }
})();

As you can see, I've rearranged some things. What is still not working is the checking for the dropdown control: For this to work, you would have to check for your default value, which would make things a bit more complicated. If you can make sure that none of the other fields can ever be set to the default value of the dropdown control, you can use the following as your "if" condition. This assumes that your dropdown default value is one space (" ") character:

if (f != null && f.type != "button" && f.required == true && (f.value == "" || f.value == " ")) {

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: