Hello all - new to these forums and to Javascript, but long-time programmer, in an unfamiliar environment. I'm trying to figure out how to detect, in Javascript, whether a field is marked as "required" in its properties. I've been googling my brains out and banging out code for hours and can't figure it out.
I assumed a simple loop through the document's fields testing for a ".required" property would do it, but the simple test below fails with the message "InvalidGetError: Get not possible, invalid or unknown." in the console, about which I could find virtually nothing:
for(var i=0; i<this.numFields; i++) { var fName = this.getNthFieldName(i); var f = this.getField(fName); if (f.required) { app.alert (fName + " is required"); } }
Simple enough, but it crashes in the first iteration with the error above.
If I introduce a typo and test for "f.requiredZZZ" instead of "f.required", it does not crash (and also doesn't work, of course, but it's a step), so would I be correct in guessing that "required" is not what I expected it to be: a boolean flag reflecting the "required" property?
Is there another way to programmatically detect whether a field is required or not? I don't want to enumerate the required fields in code, there will be dozens of different documents to be maintained by non-programmers. It has to detect fields with the "required" checkbox checked in the field properties.
Hope this is a simple problem brought on by my unfamiliarity with the operating environment of Javascript in this context.
Thanks for any tips.
Do you know what field is causing the error?
What is the type of field?
The Acrobat JS API Reference also doucments the field(s) that do not have the 'required' property. So you will need to modify your script to only test for the fields that support the 'required' property.
One can use the following JS to isolate the problem fields and provide some information about the problem and then you can use a variation in production to prevent your script from crahsing.
Using JavaScript's 'try/catch' statment make debugging a lot easier than not having it, as it traps the error and lets one continue the scirpt to the end.
George Kaiser