Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

Making fields read-only

Explorer ,
Jul 25, 2023 Jul 25, 2023

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;
        }
      }

 

TOPICS
JavaScript , PDF , PDF forms
1.4K
Translate
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
1 ACCEPTED SOLUTION
Explorer ,
Jul 28, 2023 Jul 28, 2023

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;
        }
      }

View solution in original post

Translate
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
Community Expert ,
Jul 25, 2023 Jul 25, 2023

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;
        }
      }

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
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
Explorer ,
Jul 25, 2023 Jul 25, 2023

Thanks for your response. The radio buttons still locks on all pages.

Translate
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
Community Expert ,
Jul 25, 2023 Jul 25, 2023

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? 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
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
Explorer ,
Jul 25, 2023 Jul 25, 2023

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.

Translate
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
Explorer ,
Jul 25, 2023 Jul 25, 2023

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.

Translate
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
Community Expert ,
Jul 26, 2023 Jul 26, 2023

Good catch.  Of course radio buttons will always be set to readonly if that is the condition. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
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
Explorer ,
Jul 26, 2023 Jul 26, 2023

Is there an easy way to exclude a specific field?  Like,

if(field == ""){
   field.readonly = false;
}

 

Translate
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
Community Expert ,
Jul 28, 2023 Jul 28, 2023

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. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
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
Explorer ,
Jul 28, 2023 Jul 28, 2023

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;
        }
      }
Translate
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
Community Expert ,
Jul 30, 2023 Jul 30, 2023
LATEST

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 " 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
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