Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

MANDATORY FIELD VALIDATION BEFORE SUBMIT

Avatar

Former Community Member

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

Avatar

Correct answer by
Former Community Member

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

1 Reply

Avatar

Correct answer by
Former Community Member

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

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----