These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Validating each line that is added in a subform.

NancyM
Registered: Sep 25 2006
Posts: 40
Answered

I have created a form in Designer 7.1 using addInstance for additional lines. The addInstance allows the user to add different US states to their setup. What I want is to check to make sure that the states are not duplicated. Is this possible and if so how is it accomplished?

My Product Information:
LiveCycle Designer, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Yes, it is possible. But it will need to be done with JavaScript.

One method would be to attach a script to the change event of the dropdown list (the list that has the states in it). The Script would loop through all of the subforms and compare the selection to the value of all the other state fields. If the selected state is found, then it would reject the users selection.

Use the resolveNodes() function to get a list of the dropdown fields you need to grab the info from.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

NancyM
Registered: Sep 25 2006
Posts: 40
I am not sure how resolveNodes() works, but I will try to figure it out. If there is a simple resource for a beginner to understand resolveNodes(), please advise.

Thanks
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Please watch this video. It'll answer some of your questions.

https://admin.adobe.acrobat.com/_a200985228/p87746471/


Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

NancyM
Registered: Sep 25 2006
Posts: 40
Thanks for the video, it did help to understand Nodes. I just don't know the correct way to compare the fields. The debugger is not showing any scripting errors, but the compare statement is not catching the duplicate selection. This is what I have now.

----- form1.AdditionalStates.detail.State::change - (JavaScript, client) -------------/// - ONLY 17 lines can be added to form
//check the number of DETAIL instances
var numberOfDetail = xfa.resolveNodes("Detail[*]").length;

//start a FOR loop on all the DETAILS
for(var i = 1 ; i <= numberOfDetail ; i++)
{////Validate State has been selected
var fState = xfa.resolveNode("form1.AdditionalStates.detail["+ (i-1) +"].State.rawValue");

if (this.rawValue == fState.rawValue)
{
app.alert("You have already selected this state");
this.rawValue = null;
xfa.host.setFocus("State");
}
}
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
You're close, but you code needs a bit of help.

First, the first line should acquire all of the fields that will be compared. And Make sure you give enough of a path, and that the spelling is correct. Once the field list is acquired there is no need to call "resolveNode".

Then you test against "xfa.event.change" which is the newly selected value.

Like this

var DetailFields = xfa.form.form1.AdditionalStates.resolveNodes("Detail[*].State");for(var i=0;i<DetailFields.length;i++){if(DetailFields.item(i).rawValue == xfa.event.change){app.alert("You have already selected this state");xfa.event.change = "";}}

When developing code its a good idea to test out ideas one at a time until you are confident you know how all the parts you'll need work. Putting together a large bunch of code with out knowing what works and what doesn't will guarentee problems.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

NancyM
Registered: Sep 25 2006
Posts: 40
I understand trying one thing at a time. So I started over. I entered the following, but it does not seem to catch the duplicate state in the dropdown box. I do not understand the second line of code (the 'for' statement). Could you break it down. I do not know what the item(i) id for.

----- form1.AdditionalStates.detail.State::change - (JavaScript, client) ---------- var DetailFields = xfa.form.form1.AdditionalStates.resolveNodes("detail[*].State");for(var i=0;i<DetailFields.length;i++){if(DetailFields.item(i).rawValue == xfa.event.change){app.alert("You have already selected this state");xfa.event.change = "";}}
scottsheck
Registered: May 3 2007
Posts: 138
Nancy,

.item(i) is a reserved word when you use the .resoveNodes() function that lets you refer to a specific item of DetailFields. If you want to debug your own code, copy your app.alert statement above your if(DetailFields...) statement to see what the values are that you are comparing in the IF() statement.

Scott
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The very first thing you need to do is validate that the first line of code really does acquire all of the "State" fields. The code I provided is correct syntactically. But whether or not it works depends on the hierarchy of your form. The names used in the path must match the exact name, case, and order of the names in the forms hierarchy.

To see exactly what these names should be do this.
1. In LiveCycle Designer, select the "State" pulldown that's in your subform.
2. Open up a scripting window and select the change event.
3. The SOM path to the "State" fields is displayed at the top of the scripting window. It should look like this.

----- Form1.AdditionalStates.Detail.State::change: - (JavaScript, client) ----

The "Form1.AdditionalStates.Detail.State" is the SOM Path. Adjust the "resolveNodes" code line to reflect the exact path that is shown.

To test the code. Put this script into the change script

var DetailFields = xfa.form.Form1.AdditionalStates.resolveNodes("detail[*].State");console.println("Fields Found: " + DetailFields.length);

This scripts performs the resolve nodes and then prints the number of fields that were returned in the console window. If it returns the correct number of state fields, then you're ready to move on to the "for" loop. If it returns 0. then something is wrong. Probably with one of the path elements.

For example, can you spot the error in the code sample above? The "d" in the word "detail" is lower case. But in the SOM path shown previously the "D" is a capital. So for the given SOM path the code is incorrect. These are the kinds of things that will cause problems. It's like a where's Waldo thing.

You can find out about using the console window here:
http://www.acrobatusers.com/tech_corners/javascript_corner/tips/2006/javascript_console/

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script