Expand my Community achievements bar.

Hidden Required fields - existing script isn't working.

Avatar

Level 2

Hello All!

I have a script that is designed to scan through the form when the users submits it and make any hidden required fields un-required. However, recently after a change to the default profile (I think this was the inciting incident, I'm not entirely sure), the user has to press the submit button twice in order for the script to work. 

I have absolutely no idea what's causing this, I poured over it all day, but can't figure it out. Any help would be greatly appreciated! Relevent code linked below:

-----------

function validateForm() {
	fixNullTest( xfa.form.form1.formPage );
	console.log("after fix null test");
	var validationReturn = !ValidationHTML.setFocusToRequired(xfa.form.form1.formPage, null, true).bool;
	console.log("after validationHTML")
	console.log("validation return: " + validationReturn)
	return validationReturn;
	
}//function

// this is called by validate form to turn off the required attributes for fields
// that are hidden
function fixNullTest( oNode ) {
	if ( oNode.presence == "hidden" || oNode.presence == "invisible" )
	{
		turnOffNullTest( oNode );
	}//if
	else 
	{
		for(var i=0; i<oNode.nodes.length; i++) 
		{
			var oChildNode = oNode.nodes.item(i);
			if ( oChildNode.className == "subform" || oChildNode.className == "exclGroup" )
			{
				fixNullTest( oChildNode );
			}//if
			else if ( oChildNode.className == "field" && (oChildNode.presence == "hidden" || oChildNode.presence == "invisible"))
			{
				turnOffNullTest( oChildNode );
			}//else if
		}//for
	}//else
}//func

function turnOffNullTest( oNode ) 
{
	//console.log("begining of turn off null test for: " + oNode.className);
	
	switch( oNode.className ) 
	{
		case "exclGroup":
			oNode.validate.nullTest = "disabled";
		case "subform":
			for(var i=0; i<oNode.nodes.length; i++) 
			{
				var oChildNode = oNode.nodes.item(i);
				turnOffNullTest( oChildNode );
			}//for
			break;
		case "field":
			switch (oNode.ui.oneOfChild.className) 
			{
				case "checkButton":
				case "choiceList":
				case "dateTimeEdit":
				case "exclGroup":
				case "imageEdit":
				case "numericEdit":
				case "passwordEdit":
				case "textEdit":
				case "signature":
					oNode.presence = "visible";
					oNode.validate.nullTest = "disabled";
					oNode.presence = "hidden";
					//oNode.presence = "inactive";
					//console.log("field has actually been turned off");
					break;
				default:
					break;
					
			}//switch
			break;
	}//switch
	
	
}//func

 The code below this is used above, but I don't think the problem lies here. This code basically just makes the text red and sets focus, but the main issue is the form not submitting when visible fields are all completed.

//function to find required fields left empty and setting the focus on them after submit

function setFocusToRequired(oNode, found, base)
{
	//console.log("Hello.  In setFocusToRequired");
	if (found == null) 
	{
		found = {};	
	}//if	
	for(var i=0; i<oNode.nodes.length; i++) 
	{
		var oChildNode = oNode.nodes.item(i);
		if (oChildNode.className == "field" || oChildNode.className == "exclGroup")
		{
			if (oChildNode.validate.nullTest == "error" && (oChildNode.rawValue == "" || oChildNode.rawValue == null))
			{
				if (oNode.className == "area")
				{
					for (var n = oNode.index; n>=0; n--)
					{
						if (oNode.parent.nodes.item(n).className == "draw")
						{
							oNode.parent.nodes.item(n).font.fill.color.value = "255,0,0";
							break;
						}//if
					}//for
				} //if
				else if (oChildNode.className == "field")
				{
					oChildNode.caption.font.fill.color.value = "255,0,0";
				} //else if
				else 
				{
					for (var n = i; n>=0; n--)
					{
						if (oNode.nodes.item(n).className == "draw")
						{
							oNode.nodes.item(n).font.fill.color.value = "255,0,0";
							break;
						}//if
					}//for
				}//else
				if (found.field == null) 
				{
					found.field = oChildNode;
				}//if
				found.bool = true;
			} //if
			else 
			{
				if (oNode.className == "area")
				{
					for (var n = oNode.index; n>=0; n--)
					{
						if (oNode.parent.nodes.item(n).className == "draw")
						{
							oNode.parent.nodes.item(n).font.fill.color.value = "0,0,0";
							break;
						}//if
					}//for
				} //if
				else if (oChildNode.className == "field")
				{
					oChildNode.caption.font.fill.color.value = "0,0,0";
				}//else if
				else 
				{
					for (var n = i; n>=0; n--)
					{
						if (oNode.nodes.item(n).className == "draw")
						{
							oNode.nodes.item(n).font.fill.color.value = "0,0,0";
							break;
						}//if
					}//for
				}//else
			}//else
		}//if
	
		if (oChildNode.className == "subform" || oChildNode.className == "area" || oChildNode.className == "exclGroup")
		{
			setFocusToRequired(oChildNode, found, false);
		}//if
	}//for
	if (base)
	{
		if (found.field != null) 
		{
			xfa.host.messageBox("At least one required field was not completed correctly. Please fill in all required fields and try to submit again.");
			xfa.host.setFocus(found.field.somExpression);
		}//if
	}//if
	return found;
}

 

2 Replies

Avatar

Level 10

Well, there're at least two problems.

console.log("after fix null test");

There's log method in Acrobat's console object, it's println instead. 

console.println("after fix null test");

 

Also, what is the reference? A scriptobject? Do you have a form you can share? That would be much easier to check.

ValidationHTML.setFocusToRequired

 

How do you call the script above? 

Avatar

Level 2

Those console.log statements weren't in there until after this started, they are just there for debugging purposes. 

ValidationHTML is another fragment that is attached to the form. The second code block is the "setFocusToRequired" part of it. This method just handles turning text red and setting the focus of the view window.

 

The issue (I think) is within the first code block, as that is the section of code that handles modifying the .nulltest property on the fields.

 

I wish I could share a form, but we have some security restrictions at my company around that sort of thing. I was mostly seeing if there was something in there syntax-wise that was wrong