Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Programmatically access "required" fields

Avatar

Level 4

Hello,

My form doesn't have an actual submit button, so the built-in "required" field function isn't of any use to me unless I can programmatically check them. 

So if I make 25 fields on page 1 required, I don't want to have a long if/then statement checking if each one has a null value.

Is there a way I can "check" to see if any required field is null? Perhaps there is some way to group fields into an array or something-but this is beyond my abilities right now.  I would appreciate any help!

1 Accepted Solution

Avatar

Correct answer by
Level 2

How have you scripted to e-mail via button?  Inherently, using the e-mail submit button or regular button with submit to URL as "mailto:username@company.com  it should check to ensure required fields are filled.

You could also simply put

 

xfa.form.form1.execValidate();  on the click event of button.  This will force a check on required fields. (assuming form1 is the name of the top most name in your) hierarchy

Or you have to do as suggested above loop through the fields check that they are required and if they are null.

View solution in original post

10 Replies

Avatar

Level 5

What you can try is keep marking the fields in designer as required and programmatically search and test if the content of these fields are null (i.e. empty). For example

var oFields = xfa.resolveNodes("form1..#field[*]");

for ( var i = 0; i < oFields.length; i++ )

{

    var oField = oFields.item(i);

    if ( oField.validate.nullTest == "error" && oField.isNull )

    {

        xfa.host.beep(3);

        xfa.host.messageBox( "Please enter value for " + oField.name );

        xfa.host.setFocus( oField );

    }

}

will loop through every field object on the form, regardless of location within the hierarchy and check if the required nullTest check is in place, if it is, checks the value of the field to see if its null.

Avatar

Level 4

Thanks for your reply dohanion.

Where should the code go exactly? It won't work putting it on an OK button that I use to email the form.

Avatar

Level 5

It shouldnt matter where it is run from. You can put it on the click event of your ok button. Just make sure it is set to javascript and that the root of the form hierarchy matches the search criteria in xfa.resolveNodes(). form1..#fields should be changed if your for has a different root name or if you want to start at a lower branch for example page, subform, etc.

Avatar

Level 4

I made a simple dummy form and I'm doing it on Page 1 right now with a "Next" OK button.  It's working fine for the first required field, but when I added more fields it only works for the first required one.

var oFields =

xfa.resolveNodes("TextField1");

xfa.resolveNodes("TextField2");

for..........

Am I doing that incorrectly?

Avatar

Level 5

resolveNodes will return a list of objects satisfying the expression, so if you tried xfa.resolveNodes("TextField1"), it will return a list of 1 element containing the first textfield1 it comes across. The sample above uses an expression "form1..#field[*]", which returns a list of all objects of type field starting at the form1 level. The double .. notation means to navigate through each child container, # represents the object type and not name and [*] means all instances.

It sounds like you are trying to map all the fields. Using the #field[*] should work without knowing the name at all.

If you are only getting a reference to one item, you would use xfa.resolveNode

Avatar

Correct answer by
Level 2

How have you scripted to e-mail via button?  Inherently, using the e-mail submit button or regular button with submit to URL as "mailto:username@company.com  it should check to ensure required fields are filled.

You could also simply put

 

xfa.form.form1.execValidate();  on the click event of button.  This will force a check on required fields. (assuming form1 is the name of the top most name in your) hierarchy

Or you have to do as suggested above loop through the fields check that they are required and if they are null.

Avatar

Level 4

Thanks for explaining that bit; however it doesn't seem to be able to search through objects not on the same page as the button where I placed the code.

So if I have required fields on form1.page1 and form1.page2, if I place:

var oFields = xfa.resolveNodes("form1..#field[*]");

........

.......

On form1.page1, it won't check any required fields on page2. 

Avatar

Level 2

// Get the field containers from each page.
If you modify to this it will check field on all pages.

for (var nPageCount = 0; nPageCount < xfa.host.numPages; nPageCount++) {
var oFields = xfa.layout.pageContent(nPageCount, "field");

//var oFields = xfa.resolveNodes("form1..#field[*]");
for ( var i = 0; i < oFields.length; i++ )
{
     var oField = oFields.item(i);
     if ( oField.validate.nullTest == "error" && oField.isNull )
     {
         xfa.host.beep(3);
         xfa.host.messageBox( "Please enter value for " + oField.name );
         xfa.host.setFocus( oField );
     }
}
}

Avatar

Level 4

xfa.form.form1.execValidate();

This worked perfectly for my purposes (and is the most efficient). I've decided to check for blanks on a page by page basis so there's no real need for .setFocus.

Thanks!

Avatar

Level 4

One more thing: Any idea how to get rid of those red lines around required fields, or only have them appear after the "check"?