getField updating AFTER validation

New Here ,
May 14, 2019 May 14, 2019

Copy link to clipboard

Copied

I'm just starting out trying to get my head wrapped around acrobat javascript to try and automate some things, and thought I should try and so some simple tests.

So, I have the following function.

function Alert () {

  console.println(doc.getField("PC").value);

}

And in the Validate tab for the "PC" field, I have set

"Run a custom validation script:"

Alert()

Yet when I look in the console, the value it prints is the value BEFORE I make a change. For example, if the value in the PC box is "Bob" and I change it "Alice", then it will print "Bob" instead of "Alice".

How can I get the new value for the field in the validate script?

TOPICS
Acrobat SDK and JavaScript

Views

98

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , May 14, 2019 May 14, 2019
The custom format script is the last to run. It doesn't have to be used to change the appearance of the field, it can do anything that you need it to do and runs after the validation script (if any) and after the field value is committed. I use it to do all sorts of stuff that has nothing to do with field formatting.

Likes

translate

Translate

Translate
Most Valuable Participant ,
May 14, 2019 May 14, 2019

Copy link to clipboard

Copied

Use event.value instead of this.getField("...").value .

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 14, 2019 May 14, 2019

Copy link to clipboard

Copied

Is there no more universal way to get this value. I am attempting to build a document model stored in global

(function () {

  global.model = new Object();

  Object.defineProperty(global.model, "pc", {

    get: function () {

      return doc.getField("PC").value;

    },

    set: function (value) {

      return doc.getField("PC").value = value;

    }

  })

})();

And would like it to always get the most up to date version of a field so that I can use a common interface inside all validation and calculate functions.

(doc is defined to be the global this variable of an Acrobat JS script)

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Most Valuable Participant ,
May 14, 2019 May 14, 2019

Copy link to clipboard

Copied

That is the most up to date value of a field; in that method you are dealing with some typed information which is not yet stored into the field. That's the point of the validation script: it protects the field from bad data, and at that point the event is only typing. Calculate functions, by contrast, are dealing with the values in OTHER fields which are already stored.

You do need to study in detail the description of each method. You may find a few simple rules that can be applied, but universality isn't a a strength of the design.

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 14, 2019 May 14, 2019

Copy link to clipboard

Copied

The issue is I want the script to run only once on edit, rather than whenever any field is updated. Is there a way to achieve this outside of a validator script?

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 14, 2019 May 14, 2019

Copy link to clipboard

Copied

The custom format script is the last to run. It doesn't have to be used to change the appearance of the field, it can do anything that you need it to do and runs after the validation script (if any) and after the field value is committed. I use it to do all sorts of stuff that has nothing to do with field formatting.

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 14, 2019 May 14, 2019

Copy link to clipboard

Copied

I'm just using it for encapsulation.

The formatter is likely the correct thing to do. What I am doing is most well suited to "calculate" as an idea, but calculate runs when any field has changed and with some fairly complex calculations running every change the sheet would lag horrifically. I'm instead going to update the fields dependent on other fields when they are changed, rather than recalculating on any change, and it seems that format is the correct time to go about doing that.

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 14, 2019 May 14, 2019

Copy link to clipboard

Copied

What exactly are you trying to achieve by using the global object? It's not as useful as you might think unless you've disabled Enhanced Security... which you should not be doing.

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Most Valuable Participant ,
May 14, 2019 May 14, 2019

Copy link to clipboard

Copied

If you only want to run it when a field is updated, why would you use the validation script at all, since we know it hasn't been updated?

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines