Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Radio button list length keeps increasing - is this right?

Avatar

Level 3

I have a set of 5 radio buttons in a form that are all within the same radio button list (as far as I can tell!) - they look like this in the Hierarchy tab:

rblApprQ1

     - rblApprQ1_1

     - rblApprQ1_2

     - rblApprQ1_3

     - rblApprQ1_4

     - rblApprQ1_5

I've got a bit of validation on my submit button to check one of the boxes has been checked, as follows:

var

rblApprQ1 = xfa.resolveNode("mainForm.rblApprQ1");

This gets passed into another script object:

function

checkRadioButtons(field)

{

     rbChosen

= '';

     len

= field.nodes.length;

    

     for (i = 0; i <len; i++)

     {

          if (field.nodes.item(i).rawValue != null)

          {rbChosen

= field.nodes.item(i).rawValue;}

     }

     if (rbChosen == '')

     {

return false;}

     else

     {

return true;}

}

The problem I have is, if no radio button has been checked, the value of len in the checkRadioButtons function is 5 - I'm presuming this is the 5 separate radio buttons in my radio button list? Yet when any of the buttons are selected, the len value goes up to 7 thus rendering my function rubbish!

Am I doing something wrong?

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

You are getting an extra node after the selection is made. If I look at the className property of the object you will see that each radiobutton has a class

name of field and the extra node is of type calculate. So put an extra check in to make sure that you are testing a node of type "field". Then I woudl move the code that returns the true or false outside of the for loop. Right now you are returning it for each radio button in your list. You only need to return a single value. I also changed the name of the passed object from field to test to make it easier to follow. Here is a view of the code that I created to test it:

checkRadioButtons(RadioButtonList)

function checkRadioButtons(test){
rbChosen = '';
len = test.nodes.length;
//app.alert(len)

for (i = 0; i <len; i++){
  rbName = test.nodes.item(i);

  //app.alert(rbName.saveXML("pretty"));
  if (rbName.className == "field"){
     if (test.nodes.item(i).rawValue != null){
    rbChosen += test.nodes.item(i).rawValue;
     }
    }
}
   if (rbChosen == ''){
    app.alert("No RBs were chosen");
   } else {
  app.alert("RB selection was made");
}
}

View solution in original post

4 Replies

Avatar

Former Community Member

There is no need for all of that code. If you click on the radioButtonList and then go into the Object Palette/Value Tab and set the Type dropdown to

User Entered - Required the product will not allow you to submit without choosing one of the radio buttons.

Paul

Avatar

Level 3

Thanks Paul but I prefer using javascript for validation purposes - yes it's long-winded but it's what I'm used to and I like the extra control over how errors are displayed, etc.

So, is there a way to do this with my code?

Avatar

Correct answer by
Former Community Member

You are getting an extra node after the selection is made. If I look at the className property of the object you will see that each radiobutton has a class

name of field and the extra node is of type calculate. So put an extra check in to make sure that you are testing a node of type "field". Then I woudl move the code that returns the true or false outside of the for loop. Right now you are returning it for each radio button in your list. You only need to return a single value. I also changed the name of the passed object from field to test to make it easier to follow. Here is a view of the code that I created to test it:

checkRadioButtons(RadioButtonList)

function checkRadioButtons(test){
rbChosen = '';
len = test.nodes.length;
//app.alert(len)

for (i = 0; i <len; i++){
  rbName = test.nodes.item(i);

  //app.alert(rbName.saveXML("pretty"));
  if (rbName.className == "field"){
     if (test.nodes.item(i).rawValue != null){
    rbChosen += test.nodes.item(i).rawValue;
     }
    }
}
   if (rbChosen == ''){
    app.alert("No RBs were chosen");
   } else {
  app.alert("RB selection was made");
}
}

Avatar

Level 3

If you're a drinker then the beers are piling up - if not, then I guess a few glasses of water are in order.

Thanks again for the help,

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----