Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

MANDATORY FIELD VALIDATION BEFORE SUBMIT

rohitacmilan
Level 4
Level 4

Hi Experts,

                  I have a requirement to check whether all the mandatory fields in a form has been filled up before submitting the form. The form has 1 page which is flowed and spans for 8 pages. There are around 40 to 50 mandatory fields. I tried putting the following script in the click event of the SUBMIT button.

var lv_txtfld;
for (var nPageCount = 0; nPageCount < xfa.host.numPages; nPageCount++) {
var oFields = xfa.layout.pageContent(nPageCount, "field");
var nNodesLength = oFields.length;
for (var nNodeCount = 0; nNodeCount < nNodesLength; nNodeCount++) {
  if (oFields.item(nNodeCount).ui.oneOfChild.className == "textEdit") {
   lv_txtfld = oFields.item(nNodeCount).ui.oneOfChild.rawValue;
   if (( !lv_txtfld.optional ) && ( lv_txtfld == null ) || ( lv_txtfld == " " )) {
    xfa.host.message(oFields.item(nNodeCount).ui+" "+"is a mandatory field - Please enter the same");

    continue;
   }
  }
}
}

The above script isnt working - I don't want to do the mandatory field check on a field by field basis as that would be too tedious for 40 - 50 fields. Please let me know of any alternate solution.

1 Accepted Solution
JoshDBoyle-DVzR
Correct answer by
Level 4
Level 4

Hello rohitacmilan!

Just to ensure I'm covering all bases, note that the existence of ANY empty required field (set as User Entered - Required) will prevent the form from submitting by default.

What I believe you're getting at though is that you would like to programmatically check all required fields prior to submission so you can take a custom action.  If this is the case, you will indeed need a script like the one you have displayed.  After reviewing your script I found a few issues that I'll discuss after displaying the solution:

var lv_txtfld;

for (var nPageCount = 0; nPageCount < xfa.host.numPages; nPageCount++)

{

     var oFields = xfa.layout.pageContent(nPageCount, "field");

     var nNodesLength = oFields.length;

     for (var nNodeCount = 0; nNodeCount < nNodesLength; nNodeCount++)

      {  

           if (oFields.item(nNodeCount).ui.oneOfChild.className == "textEdit")   

           {  

                lv_txtfld = oFields.item(nNodeCount);  

               if ( (lv_txtfld.mandatory == "error") &&

                    (lv_txtfld.rawValue == null || lv_txtfld.rawValue == ""))   

               {    

                     xfa.host.messageBox(lv_txtfld.name + " is a required field - please enter the same");  

               }             

           }

     }

}

The issues were as follows:

1) The line that reads "lv_txtfld = oFields.item(nNodeCount).ui.oneOfChild.rawValue" was bombing out.  What I believe was the intention here was to get a reference to the specific node we are on within our loop for ease of use in the following if statement.  We should note however that we already have an array of references to all applicable nodes in the array "oFields".

2) The line that starts with "if (( !lv_txtfld.optional ) ..." was bombing out for a few reasons.  First and foremost, "optional" is not a property of a form node.  I believe the intention here was to check if the current node was required or not.  In that case, you would normally check the "mandatory" property.  A field that is required will have a "mandatory" property equivalent to the string literal "error".  Secondly, the segment of the "if" clause after the "&&" appears to be written with the intent of checking the contents of the current node for a value.  If that's the case we need to check the "rawValue" property.  In addition to this point, we would want the second clause of our "||" statement to not have whitespace in the string literal as "" is not equivalent to " ".

3) It appears that the intent of the line within the innermost "if" statement is to display a message box to the user to let them know that an empty, required field was found.  If this is the case, then we can simply use the "name" property of the current node instead of "oFields.item(nNodeCount).ui".  Further, the method to display a message box is "xfa.host.messageBox(...)".

4) Although not really an "error" per se, I thought I'd point out that you don't need to use the keyword "continue" after you display the message box to the user about an empty required field.  You are within a for loop at this point and it will continue to run normally until at last its test clause fails.

I hope I've been helpful!  Please do let me know if you have any questions.

Take care!

Josh Boyle

jboyle@cardinalsolutions.com

Cardinal Solutions Group

View solution in original post

0 Replies
JoshDBoyle-DVzR
Correct answer by
Level 4
Level 4

Hello rohitacmilan!

Just to ensure I'm covering all bases, note that the existence of ANY empty required field (set as User Entered - Required) will prevent the form from submitting by default.

What I believe you're getting at though is that you would like to programmatically check all required fields prior to submission so you can take a custom action.  If this is the case, you will indeed need a script like the one you have displayed.  After reviewing your script I found a few issues that I'll discuss after displaying the solution:

var lv_txtfld;

for (var nPageCount = 0; nPageCount < xfa.host.numPages; nPageCount++)

{

     var oFields = xfa.layout.pageContent(nPageCount, "field");

     var nNodesLength = oFields.length;

     for (var nNodeCount = 0; nNodeCount < nNodesLength; nNodeCount++)

      {  

           if (oFields.item(nNodeCount).ui.oneOfChild.className == "textEdit")   

           {  

                lv_txtfld = oFields.item(nNodeCount);  

               if ( (lv_txtfld.mandatory == "error") &&

                    (lv_txtfld.rawValue == null || lv_txtfld.rawValue == ""))   

               {    

                     xfa.host.messageBox(lv_txtfld.name + " is a required field - please enter the same");  

               }             

           }

     }

}

The issues were as follows:

1) The line that reads "lv_txtfld = oFields.item(nNodeCount).ui.oneOfChild.rawValue" was bombing out.  What I believe was the intention here was to get a reference to the specific node we are on within our loop for ease of use in the following if statement.  We should note however that we already have an array of references to all applicable nodes in the array "oFields".

2) The line that starts with "if (( !lv_txtfld.optional ) ..." was bombing out for a few reasons.  First and foremost, "optional" is not a property of a form node.  I believe the intention here was to check if the current node was required or not.  In that case, you would normally check the "mandatory" property.  A field that is required will have a "mandatory" property equivalent to the string literal "error".  Secondly, the segment of the "if" clause after the "&&" appears to be written with the intent of checking the contents of the current node for a value.  If that's the case we need to check the "rawValue" property.  In addition to this point, we would want the second clause of our "||" statement to not have whitespace in the string literal as "" is not equivalent to " ".

3) It appears that the intent of the line within the innermost "if" statement is to display a message box to the user to let them know that an empty, required field was found.  If this is the case, then we can simply use the "name" property of the current node instead of "oFields.item(nNodeCount).ui".  Further, the method to display a message box is "xfa.host.messageBox(...)".

4) Although not really an "error" per se, I thought I'd point out that you don't need to use the keyword "continue" after you display the message box to the user about an empty required field.  You are within a for loop at this point and it will continue to run normally until at last its test clause fails.

I hope I've been helpful!  Please do let me know if you have any questions.

Take care!

Josh Boyle

jboyle@cardinalsolutions.com

Cardinal Solutions Group

View solution in original post