Expand my Community achievements bar.

SOLVED

How to make a text field required at run time when the user clicks the checkbox ?

Avatar

Level 9

I got a form where , there are several checkboxes and text fields associated with that checkboxes.If the use clicks on the check box then the associated

text fields should become required.I have tried the change event and the click event for the checkboxes.It gives error in the onChnage event and I got some weird results in the on click event .Sometimes if the user click the check box , then for the 1st time the field doesnt become required , then the user onclicks the check box and when the user clicks the checkbox for the second time the field becomes required,but if i deselects the highlighted field option and selects it again the field becomes required even if the user onchecks it I am totally confused !!! Adding to it i have also written the wrong code still i achieved the desired result ?? How it can be possible . I am writing the codes below , please help me as i am working under a deadline from the client.

onChange event :

var newVal = this.boundItem(xfa.event.newText);
if(newVal == 0)
  {
    xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row4.Purchasecost3_1").validate.nullTest = "disabled";
   }
  else
    {
     xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row4.Purchasecost3_1").validate.nullTest = "error";
     }*/

For the onClick event :

var a = xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row2.CostItemRecovered_OnlyEnergyCharges").rawValue;
//app.alert(a);
if(a == 0)
  {
    xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row4.ComplianceCheck_L1_1").validate.nullTest = "error";
   }
  if(a == 1)
    {
      xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row4.ComplianceCheck_L1_1").validate.nullTest = "disabled";
     }

   Please help someone !!!

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi,

You might have this solved at this stage, but here are some pointers.

An example of looping through a dynamic table is here: Re: How do you make columns invisible/visible or hidden with checkbox?

Generally you do not need to resolve nodes, especially when the object with script is in the same table as the target objects.

For example if the table was static your script could be simplified:

if (this.rawValue == "1")  // this object is in Row2
{
     Row4.ComplianceCheck_L1_1.mandatory= "error";

...

}

However because you will be looping through all of the repeatable instances of the table/rows then you will need to resolve the nodes. Have a look at the example above.

In your script you have Row4 to Row8. I don't know if all of these are repeatable or just some of them. If we take Row4 as being repeatable then you can apply the following to all repeatable rows.

var vRow4 = Table48._Row4.count;

console.println("Row4: " + vRow4); // You can delete this or comment it out after testing

if (this.rawValue == "1")  // this object is in Row2

{

     for (var i=0; i<vRow4; i++)

     {

          xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row4[" + i + "].ComplianceCheck_L1_1").mandator = "error";

     }

...

}

else

{

     for (var i=0; i<vRow4; i++)

     {

          xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row4[" + i + "].ComplianceCheck_L1_1").mandator = "disabled";

     }

...

}

I haven't tested this, but it should be close to working.

Good luck,

Niall

View solution in original post

4 Replies

Avatar

Level 10

Hi,

I tend to use the mouseUp event with checkboxes (using the .rawValue).

Good luck,

Niall

Avatar

Level 9

I used the onchange event and wrote the code for it. I got the result . But there is a problem.All these fields are in a table and it's a dynamic table where you can add rows dynamically at the run time .I have other change event for some fields in this table .All are working fine when i add the row.But for this check box the required fields are not validated as required at the run time . I am here giving the code , Please help me .

form1.Subform0.Subform1.Subform9.Subform10.Table48.Row2.CostItemRecovered_OnlyEnergyCharges::change - (JavaScript, client)
    
if(this.rawValue == "1")
{
  xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row4.ComplianceCheck_L1_1").mandatory= "error";
  xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row4.ComplianceCheck_L2_1").mandatory= "error";
  xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row5.ComplianceCheck_L1_2").mandatory= "error";
  xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row5.ComplianceCheck_L2_2").mandatory= "error";
  xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row6.ComplianceCheck_L1_3").mandatory= "error";
  xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row6.ComplianceCheck_L2_3").mandatory= "error";
  xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row7.ComplianceCheck_L1_4").mandatory= "error";
  xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row7.ComplianceCheck_L2_4").mandatory= "error";
  xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row8.ComplianceCheck_L1_5").mandatory= "error";
  xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row8.ComplianceCheck_L2_5").mandatory= "error";

}
else
{
xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row4.ComplianceCheck_L1_1").mandator = "disabled";
xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row4.ComplianceCheck_L2_1").mandatory = "disabled";
xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row5.ComplianceCheck_L1_2").mandatory = "disabled";
xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row5.ComplianceCheck_L2_2").mandatory = "disabled";
xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row6.ComplianceCheck_L1_3").mandatory = "disabled";
xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row6.ComplianceCheck_L2_3").mandatory = "disabled";
xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row7.ComplianceCheck_L1_4").mandatory = "disabled";
xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row7.ComplianceCheck_L2_4").mandatory = "disabled";
xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row8.ComplianceCheck_L1_5").mandatory = "disabled";
xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row8.ComplianceCheck_L2_5").mandatory = "disabled";

}

Avatar

Level 10

Hi,

I am about to run out the door!

If the table is dynamic then the issue is that the script does not know which instance of the repeating row that you are referring to: Row4[0], Row4[1], Row4[2],..., Row4[n]. This would apply to all rows that are repeatable.

Basically you need to set up your script so that first it checks how many instances of the row and then loops through each instance and applies the logic for each instance in turn.

I am trying to get some examples but they probably don't deal with exactly what you want. You could try a search on the forums for looping through instances:

https://acrobat.com/#d=z-wy195Ia4GF2hTGmZZbyA

I know that there are better examples on the forum.

Sorry I can't help more at this stage,

Niall

Avatar

Correct answer by
Level 10

Hi,

You might have this solved at this stage, but here are some pointers.

An example of looping through a dynamic table is here: Re: How do you make columns invisible/visible or hidden with checkbox?

Generally you do not need to resolve nodes, especially when the object with script is in the same table as the target objects.

For example if the table was static your script could be simplified:

if (this.rawValue == "1")  // this object is in Row2
{
     Row4.ComplianceCheck_L1_1.mandatory= "error";

...

}

However because you will be looping through all of the repeatable instances of the table/rows then you will need to resolve the nodes. Have a look at the example above.

In your script you have Row4 to Row8. I don't know if all of these are repeatable or just some of them. If we take Row4 as being repeatable then you can apply the following to all repeatable rows.

var vRow4 = Table48._Row4.count;

console.println("Row4: " + vRow4); // You can delete this or comment it out after testing

if (this.rawValue == "1")  // this object is in Row2

{

     for (var i=0; i<vRow4; i++)

     {

          xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row4[" + i + "].ComplianceCheck_L1_1").mandator = "error";

     }

...

}

else

{

     for (var i=0; i<vRow4; i++)

     {

          xfa.resolveNode("form1.Subform0.Subform1.Subform9.Subform10.Table48.Row4[" + i + "].ComplianceCheck_L1_1").mandator = "disabled";

     }

...

}

I haven't tested this, but it should be close to working.

Good luck,

Niall