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

How to detect "required" field property in form?

slinberg
Registered: Jun 27 2009
Posts: 5

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.

My Product Information:
Acrobat Pro 9.1.1, Macintosh
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
Having written a function to test all of the fields in a form for being required, I can tell you that not all fields have the 'required' property and trying to test one of these field types for this property will throw an error.

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.
function TestRequired(oField) {// test to see if the field object is a required fieldvar bRequired = false; // set logical result of testtry {// try possible problem codebRequired = oField.required} catch(e) {// caught error// print i, field name, and field type instead of system errorconsole.println('Error at : ' + i + ': ' + fName + ' type of field: ' + f.type );} // end try catchreturn bRequired;} // end function for(var i=0; i<this.numFields; i++){var fName = this.getNthFieldName(i);var f = this.getField(fName);// test for required fieldif(TestRequired(f)) {app.alert (fName + " is required");} // end required true } // end for number of fields

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

slinberg
Registered: Jun 27 2009
Posts: 5
Aha - that would explain it, for sure.

Thanks very much, and thanks for the sample code... I'll bang on it and this should get it going. Cheers!

(Edit: worked fine on the first run - thanks again! And the fields choking on the .required property were buttons.)
Boxer
Registered: Apr 23 2010
Posts: 1
I've used this method in the submit button of a 3-page form to create an array of field names. Now, how can I sort this array by tab order? The order generated by getNthFieldName has its own order (apparently alphanumeric) which doesn't correspond with tab order.
try67
Expert
Registered: Oct 30 2008
Posts: 2399
A script has no access to the tab order.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com