Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

How to distinguish form element as textfield

Avatar

Former Community Member

I'm using the following script to disable all the elements in the page:

    for (var nPageCount = 0; nPageCount < xfa.host.numPages; nPageCount++) {

    var oFields = xfa.layout.pageContent(nPageCount, "field");
    var nNodesLength = oFields.length;
    // Set the field property.
    for (var nNodeCount = 0; nNodeCount < nNodesLength; nNodeCount++) {
    if(oFields.item(nNodeCount).access != "readOnly")
        {
        oFields.item(nNodeCount).access = "readOnly";
        }
    }

I have a requirement where i have to disable only the textfields. So, can anyone please help me with how to get the field type of a particular field.

Thanks.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Paul's approach is good. But it impose the developer to follow a specific naming standard.

Here is my code which makes the TextFields fields readOnly irrespective of the field name.

for (var nPageCount = 0; nPageCount < xfa.host.numPages; nPageCount++)
    {
         var oFields = xfa.layout.pageContent(nPageCount, "field");
         var nNodesLength = oFields.length;
        
         for (var nNodeCount = 0; nNodeCount < nNodesLength; nNodeCount++)
         {
               var ref = oFields.item(nNodeCount).getElement("ui");
              
              if(ref.nodes.item(0).className=="textEdit")                    // If the field is a TextBox, then proceed
              {
                   if(oFields.item(nNodeCount).access != "readOnly")
                   {
                       // Set the field property.
                        oFields.item(nNodeCount).access = "readOnly";
                   }
              }
         }
    }

Let me know if this what you want..

Nith

View solution in original post

4 Replies

Avatar

Level 10

Hi,

The className will return "field" for a textfield, but also other objects like numericfield, etc.

If you have a look at Paul's LockAllFields you can see that he doesn't lock "Fields" where the first six letters of the object's name is "Button". You could take a similar approach where all textfields have "TextField" in their name (preferably at the start of the object name). You would then test the object name as well as the object className.

Paul's example form is here: Re: Saving Fillable Form as non-fillable PDF

The section around line 30 would look like:

else if(currentElement.className == "field"){

     //Check to see if the field is a textfield - eg lock only textfields

     temp =  currentElement.name;

     if (temp.substring(0,9) == "TextField"){

          currentElement.access = "readOnly";

     }    

}

Hope that helps,

Niall

Avatar

Correct answer by
Level 10

Paul's approach is good. But it impose the developer to follow a specific naming standard.

Here is my code which makes the TextFields fields readOnly irrespective of the field name.

for (var nPageCount = 0; nPageCount < xfa.host.numPages; nPageCount++)
    {
         var oFields = xfa.layout.pageContent(nPageCount, "field");
         var nNodesLength = oFields.length;
        
         for (var nNodeCount = 0; nNodeCount < nNodesLength; nNodeCount++)
         {
               var ref = oFields.item(nNodeCount).getElement("ui");
              
              if(ref.nodes.item(0).className=="textEdit")                    // If the field is a TextBox, then proceed
              {
                   if(oFields.item(nNodeCount).access != "readOnly")
                   {
                       // Set the field property.
                        oFields.item(nNodeCount).access = "readOnly";
                   }
              }
         }
    }

Let me know if this what you want..

Nith