Expand my Community achievements bar.

SOLVED

Use same script in different fields?

Avatar

Level 4

Is there a possibility to use the same script in different fields?

I got several scripts that will validate and change the user entries on different fields. Since they all do exactly the same thing, isn't it possible to just take the "real" script out on one field and refer on other fields to this script?

1 Accepted Solution

Avatar

Correct answer by
Level 4

Sure - we have a "commonScriptObjects.xdp" fragment that we keep all of

our common javascript methods in. Then we add it to the form and hide it,

then use the following reference to point to the script object:

//GET A HANDLE TO THE SCRIPT OBJECT

var commonScript =

xfa.resolveNode("commonScriptObjectsFrag.fragmentScript");

//CALL A JAVASCRIPT METHOD IN THE SCRIPT OBJECT

commonScript.setupSearch();

Here's another thing we do to validate our required fields. We pass in

the actual field to validate and the name of the field as a text string:

if (!commonScript.validateRequired(mainContentSubform.customerName,

"Customer")) return false;

and the method to validateRequired, which is in our commonScriptObject xdp

file, looks like this:

// validate required fields

function validateRequired(oField, fieldName) {

console.println("enter validateRequired");

var commonScript =

xfa.resolveNode("commonScriptObjectsFrag.fragmentScript");

if (commonScript.isNullValue(oField.rawValue))

{

xfa.host.messageBox("Please complete the '" + fieldName +

"' field.", "Error Message", 3);

xfa.host.setFocus(oField);

return false;

}

return true;

}

Hope that helps,

Elaine

ocen12 <forums@adobe.com>

07/29/2009 09:26 AM

Please respond to

clearspace-510205244-333209-2-2140242@mail.forums.adobe.com

To

Amal Schmitz <akschmitz@comerica.com>

cc

Subject

Use same script in different fields?

Is there a possibility to use the same script in different fields?

I got several scripts that will validate and change the user entries on

different fields. Since they all do exactly the same thing, isn't it

possible to just take the "real" script out on one field and refer on

other fields to this script?

View solution in original post

5 Replies

Avatar

Correct answer by
Level 4

Sure - we have a "commonScriptObjects.xdp" fragment that we keep all of

our common javascript methods in. Then we add it to the form and hide it,

then use the following reference to point to the script object:

//GET A HANDLE TO THE SCRIPT OBJECT

var commonScript =

xfa.resolveNode("commonScriptObjectsFrag.fragmentScript");

//CALL A JAVASCRIPT METHOD IN THE SCRIPT OBJECT

commonScript.setupSearch();

Here's another thing we do to validate our required fields. We pass in

the actual field to validate and the name of the field as a text string:

if (!commonScript.validateRequired(mainContentSubform.customerName,

"Customer")) return false;

and the method to validateRequired, which is in our commonScriptObject xdp

file, looks like this:

// validate required fields

function validateRequired(oField, fieldName) {

console.println("enter validateRequired");

var commonScript =

xfa.resolveNode("commonScriptObjectsFrag.fragmentScript");

if (commonScript.isNullValue(oField.rawValue))

{

xfa.host.messageBox("Please complete the '" + fieldName +

"' field.", "Error Message", 3);

xfa.host.setFocus(oField);

return false;

}

return true;

}

Hope that helps,

Elaine

ocen12 <forums@adobe.com>

07/29/2009 09:26 AM

Please respond to

clearspace-510205244-333209-2-2140242@mail.forums.adobe.com

To

Amal Schmitz <akschmitz@comerica.com>

cc

Subject

Use same script in different fields?

Is there a possibility to use the same script in different fields?

I got several scripts that will validate and change the user entries on

different fields. Since they all do exactly the same thing, isn't it

possible to just take the "real" script out on one field and refer on

other fields to this script?

Avatar

Level 4

Thanx

I'll have a closer look to this one in some time, since right now I don't have much time (and I never worked with fragments before).

Avatar

Level 10

Why not use the actual Script Objects?

Avatar

Level 4

Jono, not sure I understand. In our fragment, we have a "fragmentScript"

script object that holds all of the common javascript, and we just add

that fragment in the forms that need the common javascript.

Avatar

Level 10

I mean the Script Objects in the Variables section of the Form Hierarchy.

I've got script fragments I'm using too but I just drag them to the Hierarchy palette (I recently found out you could do that) and reference them directly.