Expand my Community achievements bar.

Need help with code that checkes to make sure all required fields are not "0"

Avatar

Level 3

I have a javascript code that I created to check for any fields that are required and have a value of "0" to display an alert, which I made for Acrobat form, I need help converting for AEM, and was hoping someone could help me out?

 

Here is the code I have:

pmp.Page9.#subform[2].check_form::mouseDown - (JavaScript, client)
var emptyFields = [];

for (var i=0; i<this.numFields; i++) {

     var f= this.getField(this.getNthFieldName(i));

     if (f.type!="button" && f.required ) {

          if (f.valueAsString==f.defaultValue) emptyFields.push(f.name);

     }

}

if (emptyFields.length>0) {

     app.alert("**The first number indicates the Objective number** \n \n You have a zero rating in the Measuring Success section for the following ratings:\n\n" + emptyFields.join("\n"));

}
else
{
app.alert("All fields have been filled out successfully, and no errors were found.", 3)
}

I was hoping to get some pointers to help me learn.

All of the drop down fields default value is "0", and I have another script, that I am trying to work on as well to convert, which will make the fields I need set to required, if this field in not empty, since we have 13 sets of questions, which don't all get used all of the time.

So here is the code that I have for the field that sets the required presence:

if(pmp.Page1.object1.kpi1.value == null)
{// Field is empty, other fields not required
pmp.Page1.object1.Table1.Row1.ratingbox1_1.presence = "optional";
pmp.Page1.object1.Table1.Row1.ratingbox1_2.presence = "optional";
pmp.Page1.object1.Table1.Row1.ratingbox1_3.presence = "optional";
pmp.Page1.object1.Table1.Row1.ratingbox1_4.presence = "optional";
pmp.Page1.object1.Table1.Row1.ratingbox1_5.presence = "optional";

}
else
{// Field has data, other feilds required
pmp.Page1.object1.Table1.Row1.ratingbox1_1.presence = "required";
pmp.Page1.object1.Table1.Row1.ratingbox1_2.presence = "required";
pmp.Page1.object1.Table1.Row1.ratingbox1_3.presence = "required";
pmp.Page1.object1.Table1.Row1.ratingbox1_4.presence = "required";
pmp.Page1.object1.Table1.Row1.ratingbox1_5.presence = "required";

}

I have been trying to modify it for AEM, but still no luck with the 2nd piece of code either.

Can someone please help me out?

5 Replies

Avatar

Employee

Why you are using the .presence? 

Presence is used to set the object’s visibility. Also, presence can only be set to these values: 

Reference_Syntax.presence = "visible | invisible | hidden | inactive"

Not the ones you are using.

Also, .value won't work in JavaScript. you need to use .rawValue, as below:

 

pmp.Page1.object1.kpi1.rawValue == null

 

 

You need to use following JavaScript to set the field as required:

 

Reference_Syntax.mandatory = "error";  

or

Reference_Syntax.validate.nullTest = "error";

Script reference: https://help.adobe.com/en_US/AEMForms/6.1/DesignerScriptingBasics/WS92d06802c76abadb57cc1b6e12a92343...

 

Avatar

Level 3

Thank you for your help, I thought i was using the right code, just looking at the dropdown values available under presence, that's why i was using that, so thank you for your help with that.  What about the first part of code?  When i click on the button it displays the 2nd alert.  Will is display correctly once I fix the second piece of code?

Avatar

Level 3

I changed the script to the following:

if(pmp.Page1.object1.kpi1.rawValue == null)
{// Field is empty, other fields not required
pmp.Page1.object1.Table1.Row1.ratingbox1_1.mandatory = "";
pmp.Page1.object1.Table1.Row1.ratingbox1_2.mandatory = "";
pmp.Page1.object1.Table1.Row1.ratingbox1_3.mandatory = "";
pmp.Page1.object1.Table1.Row1.ratingbox1_4.mandatory = "";
pmp.Page1.object1.Table1.Row1.ratingbox1_5.mandatory = "";

}
else
{// Field has data, other feilds required
pmp.Page1.object1.Table1.Row1.ratingbox1_1.mandatory = "error";
pmp.Page1.object1.Table1.Row1.ratingbox1_2.mandatory = "error";
pmp.Page1.object1.Table1.Row1.ratingbox1_3.mandatory = "error";
pmp.Page1.object1.Table1.Row1.ratingbox1_4.mandatory = "error";
pmp.Page1.object1.Table1.Row1.ratingbox1_5.mandatory = "error";

}

It works now, however anything I put in the kpi1 field and click or tab out displays an alert stating the value in this field is invalid, even though there are no constraints on what someone can input, so how can I fix that?

Avatar

Level 10

Keep in mind: To test for an empty field you better use the isNull method or the operator === instead of ==. The latter can create false positives since it doesn't compare the types of objects.

 

if (pmp.Page1.object1.kpi1.isNull) {
     // do something
}

or

if(pmp.Page1.object1.kpi1.rawValue === null) {
    // do something
}

 

Avatar

Level 3

ok thank you for your help.

 

I found and tweaked the following code to help with the first code I posted in my original post

var EmptyFields= new Array();
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 != "button") && (oFields.item(nNodeCount).mandatory == "error" ||oFields.item(nNodeCount).rawValue == "0"))

{
EmptyFields.push(oFields.item(nNodeCount).name);
}
}
}
if (EmptyFields.length>0)

but I am struggling with trying to modify it to only look at mandatory drop down fields to report back.  I thought the above code would do that, but in playing around with it, it reports back the mandatory fields, as well as all fields with "0"