Copy link to clipboard
Copied
I have the code below on a pdf form with text fields and radio buttons over multiple pages. The code makes all text fields on the current and prior pages read-only as expected. However, the radio buttons on all pages are read-only. How do I change the code to make only the radio buttons on the current and prior pages read-only.
var currentPageNum = event.target.page;
for (var i = 0; i < this.numFields; i++){
var currentField = this.getField(this.getNthFieldName(i));
if (currentField.page <= currentPageNum || currentField.type == "radiobutton"){
currentField.readonly = true;
}
}
Copy link to clipboard
Copied
I was able to get what I was looking for with the code below. I'm not sure if it's the best way, but it works.
var currentPageNum = event.target.page;
for (var i = 0; i < this.numFields; i++){
var currentField = this.getField(this.getNthFieldName(i));
if (currentField.value != "" && GetFirstFieldPage(currentField) == 0){
currentField.readonly = true;
this.getField("field1").readonly = true;
}
}
var currentPageNum = event.target.page;
for (var i = 0; i < this.numFields; i++){
var currentField = this.getField(this.getNthFieldName(i));
if (GetFirstFieldPage(currentField) <= currentPageNum && GetFirstFieldPage(currentField) != 0){
currentField.readonly = true;
currentField.display = display.visible;
}
}
Copy link to clipboard
Copied
The problem with this code is that the "field.page" property is only a number value when there is only one widget for the field. If the field has more than one widget it is an array of page numbers. One for each widget. By definition, a radio button group has multiple widgets. If you didn't know, a widget is the "instance" of a specific field, i.e., All fields in a radio button group have the same field name, hence they are all the same field. Each button is a widget of that field.
So the solution is to create a function that returns the page of a field, regardless of the number of widgets.
function GetFirstFieldPage(oField)
{
return (typeof(oField.page)=="number")?oField.page:oField.page[0];
}
Make this a document level function.
Now here's your changed code.
var currentPageNum = event.target.page;
for (var i = 0; i < this.numFields; i++){
var currentField = this.getField(this.getNthFieldName(i));
if (GetFirstFieldPage(currentField) <= currentPageNum || currentField.type == "radiobutton"){
currentField.readonly = true;
}
}
Copy link to clipboard
Copied
Thanks for your response. The radio buttons still locks on all pages.
Copy link to clipboard
Copied
Did you validate (i.e. test) the new function?
Have you done any debug?
Are any of the radio buttons duplicated on different pages?
Where was the original code placed?
Copy link to clipboard
Copied
It's a mouse up event for a specific field. I know that I'm not specifying that the radiobutton should be on or before the current page. I don't know how to do this. I have it written as a field on the current or prior pages OR a radiobutton. I don't know how to change it to a field or a radiobutton on the current or prior page.
Copy link to clipboard
Copied
Ah, I got it. All I had to do was delete the or radiobutton from the code you provided. I had tried it that way before using the first code I posted and couldn't get it to work. Thanks.
Copy link to clipboard
Copied
Good catch. Of course radio buttons will always be set to readonly if that is the condition.
Copy link to clipboard
Copied
Is there an easy way to exclude a specific field? Like,
if(field == ""){
field.readonly = false;
}
Copy link to clipboard
Copied
Yes, fields can be excluded using any field property. For example here's a modification that uses the field name.
if ( (currentField.name != "MyField") && (GetFirstFieldPage(currentField) <= currentPageNum)){
currentField.readonly = true;
}
If you want to exclude a group of fields it might be better to create a function for identifying them. Or name the fields in a way that is easy to identify in a single code statement.
Copy link to clipboard
Copied
I was able to get what I was looking for with the code below. I'm not sure if it's the best way, but it works.
var currentPageNum = event.target.page;
for (var i = 0; i < this.numFields; i++){
var currentField = this.getField(this.getNthFieldName(i));
if (currentField.value != "" && GetFirstFieldPage(currentField) == 0){
currentField.readonly = true;
this.getField("field1").readonly = true;
}
}
var currentPageNum = event.target.page;
for (var i = 0; i < this.numFields; i++){
var currentField = this.getField(this.getNthFieldName(i));
if (GetFirstFieldPage(currentField) <= currentPageNum && GetFirstFieldPage(currentField) != 0){
currentField.readonly = true;
currentField.display = display.visible;
}
}
Copy link to clipboard
Copied
While this code works for what you want. it is very inefficient. First, two separate loops are unnecessary, and creates the possibilty that the code could be working at cross purposes. The if statements can be combined using an "or" operator, or an "else" whichever provides the correct operation. I haven't worked it out, but a two level if might be better since page 0 is alway <= to the current page.
The next inefficientcy is setting "field1" to readOnly in the loop.
The other issue is setting "