These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Radio reports false when nothing selected, need "Not Selected" instead

Hoyane
Registered: Nov 26 2011
Posts: 33
Answered

I have an issue where if no radio is selected it still returns "false"
I read in the forum that if nothing is selected on radio or checkbox it returns value of "Off" so i tried adding code to convert that to "Not Selected" but it did not work but it still says false. What am i missing?
 
Also of small note: I know the true/false is switched with Yes/No but i couldn't figure out how to assign values to each radio button. It seems the first radio button in code by default returns false and i wanted the java window radios to be Yes/No instead of No/Yes. I can live with it though. Just thought I would explain why they were switched. I really just need the radio to report "Not Selected" to my text field. Thanks for any help i may receive!
 
var textValue2;
if (testValue2==false) textValue2 = "Yes";
else if (testValue2==true) textValue2 = "No";
else if (testValue2==off) textValue2 = "Not Selected";
this.getField("Outsourced").value = textValue2;

My Product Information:
Acrobat Pro 10.1, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
A radio button group will return the button value of the selected button, or the string "Off" if none are selected. To set the button value, open up the field properties dialog for the radio button and set the "Button Value" on the Options tab.

The code you then use will depend on what you used for the buttons values and where you want to place it. For what you're talking about, you would typically use a custom calculate script for the text field, something like:

// Get the value of the radio button
var v = getField("radio1").value;

// Set the value of this text field
event.value = v === "Off" ? "Not selected" : v;


This assumes that you set the Button Value for each radio button to whatever it is you want to display when it's selected. Change "radio1" in the code about to the actual names you gave to each radio button in the group.
Hoyane
Registered: Nov 26 2011
Posts: 33
I am actually doing this in a custom calculation script, that looks like validation if I'm correct. How can I do that in calculation script?
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
As I indicated, it is a calculate script.
Hoyane
Registered: Nov 26 2011
Posts: 33
I am sorry i should have indicated that i am a complete newb.
1st the code you provided does not look like any code i've used in calculation script up to this point. It looks like validation script. But me knowing nothing i went ahead and tried that code in my calculation script but to me it doesn't match the way things look thus far so I am unsure where to place it. But again, I am a newb. I consider myself very lucky that i got this script together with minimal help while having zero java experience, but i am unable to get this one figured out after over 200 tests(no joke) to get this right(the entire project).
Here is my script. I would be eternally grateful if you would tell me exactly what to change. I know people are encouraged to figure things out while being pointed in right direction, but i am sure i am at the end of my ability with this one. Again i knew nothing about java before starting this and probably should not have gotten this far and this is the LAST thing to do on this entire project. I hate to be that guy but please finish it for me!? I've put tons of time into researching this project to solve problems along the way but I am at wits end.
So please tell me what to change withing my script and what to take out. Thanks again for helping thus far.



// Dialog Definition
var Dialog = {
// This dialog box is called when the dialog box is created
strName: "",
strName2: "",
initialize: function(dialog)
{
this.loadDefaults(dialog);
dialog.load({"date":this.strName});

},
commit: function(dialog)
{
var results = dialog.store();
testValue = results["txt2"];
testValue2 = results["txt3"];
this.datebilled = results["txt4"];
var data = dialog.store();
this.strName2 = data["txt4"];
this.strName = data["date"];
var elements = dialog.store()["txt2"];
for(var i in elements) {
if ( elements[i] > 0 ) {
testValue = elements[i];
}
}
},
loadDefaults: function (dialog) {
dialog.load({
txt2:
{
"*Not Specified": +1,
"Donations": -2,
"Office Supplies": -3,
"Outsourced Services": -4,
"Paper": -5,
"Telecommunications": -6,
"Utilities": -7
}
})
},
description:
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
type: "ok_cancel",
},
]
},
]
},
]
}
};

if(event.source.forReal && (event.source.stampName == "#Billing"))
{
Dialog.strName = util.printd("mm/dd/yyyy",new Date());


if ("ok" == app.execDialog(Dialog))
{

this.getField("date").value = Dialog.strName;

var textValue;
if (testValue==1) textValue = "Not Specified";
else if (testValue==2) textValue = "Donations";
else if (testValue==3) textValue = "Office Supplies";
else if (testValue==4) textValue = "Outsourced Services";
else if (testValue==5) textValue = "Paper";
else if (testValue==6) textValue = "Telecommunications";
else if (testValue==7) textValue = "Utilities";
this.getField("GLCode").value = textValue;

var textValue2;
if (testValue2==false) textValue2 = "Yes";
else if (testValue2==true) textValue2 = "No";
else if (testValue2==off) textValue2 = "Not Selected";
this.getField("Outsourced").value = textValue2;

this.getField("datebilled").value = Dialog.strName2;

}
}


George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
I see. The problem is I didn't know you were using a custom dialog with radio buttons, so I assumed you were asking about radio button form fields. These two types of radio buttons are entirely different things so they are necessarily handled differently. Unfortunately, I have to sleep now, but I'll take a look again tomorrow. If anyone can help in the mean time, please do.
Hoyane
Registered: Nov 26 2011
Posts: 33
So i figured out how to give each radio a unique id and how to call for it. I replaced the testValue2 part of the code with the strGRP1 code. I tried to apply a value conversion for the returned id's but it doesn't work.

The code below the XXXXX returns Rad1, Rad2, Rad3 just fine. This is the value conversion field i put in, but again the value conversion is not working.
Can anyone see the problem?

var strGRP1;
if (strGRP1==Rad1) textValue = "Not Specified";
else if (strGRP1==Rad2) textValue = "Donations";
else if (strGRP1==Rad3) textValue = "Office Supplies";
this.getField("Outsourced").value = strGRP1;

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

// Dialog Definition
var Dialog = {
// This dialog box is called when the dialog box is created
DoDialog: function(){return app.execDialog(this);},
strGRP1:"",
strName: "",
strName2: "",
GetRadioSel:function(oRslts,aCtrls){
for(var strRtn=aCtrls[0];aCtrls.length>0;strRtn=aCtrls.pop()){
if(oRslts[strRtn] == true)
return strRtn;
}
return "";
},
initialize: function(dialog)
{
this.loadDefaults(dialog);
dialog.load({"date":this.strName});

},
commit: function(dialog)
{
var oRslt = dialog.store();
var results = dialog.store();
testValue = results["txt2"];
strGRP1 = this.GetRadioSel(oRslt,["Rad1","Rad2","Rad3"]);
this.datebilled = results["txt4"];
var data = dialog.store();
this.strName2 = data["txt4"];
this.strName = data["date"];

var elements = dialog.store()["txt2"];
for(var i in elements) {
if ( elements[i] > 0 ) {
testValue = elements[i];
}
}
},
loadDefaults: function (dialog) {
dialog.load({
txt2:
{
"*Not Specified": +1,
"Donations": -2,
"Office Supplies": -3,
"Outsourced Services": -4,
"Paper": -5,
"Telecommunications": -6,
"Utilities": -7
}
})
},
description:
XXXXXXXXXXXXXXXXXXXXXXX
type: "ok",
},
]
},
]
},
]
}
};

if(event.source.forReal && (event.source.stampName == "#Billing"))
{
Dialog.strName = util.printd("mm/dd/yyyy",new Date());


if ("ok" == app.execDialog(Dialog))

{

this.getField("date").value = Dialog.strName;

var textValue;
if (testValue==1) textValue = "Not Specified";
else if (testValue==2) textValue = "Donations";
else if (testValue==3) textValue = "Office Supplies";
else if (testValue==4) textValue = "Outsourced Services";
else if (testValue==5) textValue = "Paper";
else if (testValue==6) textValue = "Telecommunications";
else if (testValue==7) textValue = "Utilities";
this.getField("GLCode").value = textValue;


this.getField("Outsourced").value = strGRP1;

this.getField("datebilled").value = Dialog.strName2;

}
}

Hoyane
Registered: Nov 26 2011
Posts: 33
Can anyone help with this? I've almost figured this out. It's just one last piece of code i think. Again After changing the code in the first post, I added a 3rd radio for "Not Specified" and I am now getting a unique return response from each radio button, Rad1, Rad2 and Rad3. I tried inserting a value conversion code but it then breaks that field. The area of concern here is the strGRP1 code.

This one is really getting me because it seems like it should be the easiest problem after learning how to get everything else working. If anyone has anything to offer weather it be an article that may help, knowing the answer or an idea of what to search for, I would really appreciate it.

Thank You
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
First, where did you get this code? It appears to be for a dynamic stamp. It is much more complex than your demonstrated understanding of JavaScript and Acrobat.

Now, "strGRP1" is a member of the "Dialog" Object, so to use it, the code has to reference the object. Just like the other members in the code.




Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Hoyane
Registered: Nov 26 2011
Posts: 33
I swear I never touched or looked at java until about 3 weeks ago. I'm just really good at figuring things out.
I also swear I am not selling this or gaining in any way. This started as just a way to exhaust creative energy.
I understand where your coming from as far as being suspect, I also realize it was stupid to just ask GEORGE to just fix it for me. I realize you guys do this to make a living and it was wrong to ask that. I've started trying to solve easier problems on the forum for other people in the hope that someone would forgive my arrogance of asking someone to do it for me, and help me solve my problem.


This is indeed a dynamic stamp and i put it together from a bunch of different sources i've found on this web site.
I used the API Reference to figure somethings like adding radios and popup lists. Otherwise there were working examples found all over this site. I just needed to figure out how to merge all the different codes.
I had help from try67 figuring 1 thing out, otherwise i figured it all out myself.

So I believe what your saying about referencing the object is this

var strGRP1;
if (strGRP1==Rad1) textValue = "Not Specified";
else if (strGRP1==Rad2) textValue = "Donations";
else if (strGRP1==Rad3) textValue = "Office Supplies";
this.getField("Outsourced").value = strGRP1;

When i add that code it no longer sends the Rad1,2,3 result anymore or at least it's not interpreted right. I will read more about dialogs.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Accepted Answer
I think you probably need to learn some programming basics before getting into this. Buy a book on JavaScript. The O'Reilly book is a good one.

strGRP1 is a memeber of the Dialog Object, so to use it the code has to reference the Dialog Object. In the context of the code that calls the dialog it is referenced like this:

Dialog.strGRP1

Inside the dialog object, as in the commit member, it is referenced like this:

this.strGRP1

Just like the other members that are used to transfer data in and out of the dialog, such as "strName".

Next, in the code in your last post there are a few errors.
1. Do not declare strGRP1.
2. The values "Rad1" "Rad2" and "Rad3" are strings, so they have to be quoted in the "if" statement.
3. The "if" is used to set the value of the variable "textValue". Isn't this the value that should be applied to the "Outsource" field?


Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Hoyane
Registered: Nov 26 2011
Posts: 33
Thanks thomp this script is finally finished. Yeah, the variable and the if statement was totally jacked. I only had to compare it to the working one already in my script, Thanks. Also it never would have worked without the quotes as well as the Dialog.strGRP1 this.strGRP1 tip, so thanks again.

Here is the completed script for anyone to use or to learn from.


var Dialog = {

DoDialog: function(){return app.execDialog(this);},

GetRadioSel:function(oRslts,aCtrls){
for(var strRtn=aCtrls[0];aCtrls.length>0;strRtn=aCtrls.pop()){
if(oRslts[strRtn] == true)
return strRtn;
}
return "";
},
initialize: function(dialog)
{
this.loadDefaults(dialog);
dialog.load({"date":this.strName});

},
commit: function(dialog)
{
var oRslt = dialog.store();
var results = dialog.store();
testValue = results["txt2"];

this.datebilled = results["txt4"];
var data = dialog.store();
this.strName2 = data["txt4"];
this.strName = data["date"];
strGRP1 = this.GetRadioSel(oRslt,["Rad1","Rad2","Rad3"]);

var elements = dialog.store()["txt2"];
for(var i in elements) {
if ( elements[i] > 0 ) {
testValue = elements[i];
}
}
},
loadDefaults: function (dialog) {
dialog.load({
txt2:
{
"*Not Specified": +1,
"Donations": -2,
"Office Supplies": -3,
"Outsourced Services": -4,
"Paper": -5,
"Telecommunications": -6,
"Utilities": -7
}
})
},
description:
XXXXXXXXXXXXXXXXXXXX
type: "ok",
},
]
},
]
},
]
}
};

if(event.source.forReal && (event.source.stampName == "#Billing"))
{
Dialog.strName = util.printd("mm/dd/yyyy",new Date());
Dialog.strGRP1 = this.strGRP1

if ("ok" == app.execDialog(Dialog))

{

this.getField("date").value = Dialog.strName;

var textValue;
if (testValue==1) textValue = "Not Specified";
else if (testValue==2) textValue = "Donations";
else if (testValue==3) textValue = "Office Supplies";
else if (testValue==4) textValue = "Outsourced Services";
else if (testValue==5) textValue = "Paper";
else if (testValue==6) textValue = "Telecommunications";
else if (testValue==7) textValue = "Utilities";
this.getField("GLCode").value = textValue;

var textValue2;
if (strGRP1=="Rad1") textValue2 = "Yes";
else if (strGRP1=="Rad2") textValue2 = "No";
else if (strGRP1=="Rad3") textValue2 = "Not Selected";
this.getField("Outsourced").value = textValue2;

this.getField("datebilled").value = Dialog.strName2;

}
}



thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Actually, the code doesn't look correct to me.

Neither "testValue" or "strGRP1" are not properly referenced in the "if" statements.

Also, this line (right before the dialog is activated), does not do anything.

Dialog.strGRP1 = this.strGRP1

At this point in the code "this.strGRP1" is undefined, it doesn't reference anything. What was your intention?

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Hoyane
Registered: Nov 26 2011
Posts: 33
The code may not look right but it all works perfectly. Not having alot of knowledge of java there may be more code in there not needed.

The strGRP1 works perfect, I am not saying it's right, just that it works.
If your referring to this part, the code actually stops working if i add "this." before strGRP1
strGRP1 = this.GetRadioSel(oRslt,["Rad1","Rad2","Rad3"]);

What i was trying to do was only have 2 radios and if neither one is selected the return value would be "Not Specified". But i couldn't figure out how to do that. Just kept returning undefined.
Then I added another radio for "Not Specified" and wanted to make it the default selected radio. I found examples of defaulted radios but it wouldn't work in my script. So I went back to just Yes/No radios (not shown in above script), let it return "undefined" and then i changed the converted text for my other field to also output "undefined" so that there was consistency in wording on the stamp when neither of the two fields were specified.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
It is some very messed up code. It works by accident.
This code, in the commit function of the dialog object:

strGRP1 = this.GetRadioSel(oRslt,["Rad1","Rad2","Rad3"]);

Sets a document object member named "strGRP1", not the dialog object member. And since "strGRP1" in the "if" statement is not properly qualified, it is also referencing the document object member. You are just lucky on this one.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Hoyane
Registered: Nov 26 2011
Posts: 33
If you don't want to help further on this it's fine as it works, however i'm not above making it right.

So i made the changes i believe your referencing..

this.strGRP1 = this.GetRadioSel(oRslt,["Rad1","Rad2","Rad3"]);


var strGRP1;
if (Dialog.strGRP1=="Rad1") Dialog.strGRP1 = "Yes";
else if (Dialog.strGRP1=="Rad2") Dialog.strGRP1 = "No";
else if (Dialog.strGRP1=="Rad3") Dialog.strGRP1 = "Not Selected";
this.getField("Outsourced").value = Dialog.strGRP1;

And it still works. I am going to go back and try inserting the code i found for having a radio selected by default that i couldnt get working before. Perhaps it was because this part wasn't exactly right.

I will be reading that debugging article you wrote tonight to see if i can learn anything from that to help myself.

Thanks again thomp for giving me direction to get it working even though it technically was not right.


XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Update: I've successfully made the changes I wanted. I found a default radio script and tried merging applicable code into mine with no success. So I started over with the default radio script and started merging my script into the one i found and it worked. After that i found out about console.println which finally allowed me to see returned values. Which allowed me to see that absolutely nothing is returned so i could write the proper if statement "" allowing me to remove the third radio altogether.
I also found some un-needed code and removed it.

This stuff is pretty fun and i may dive deeper into learning it.
THANKS to thomp & try67 for tips on the finishing touches.XXXXXXXXXXXXXXXXXXXXXXXXX

var Dialog = {
GetRadioSel:function(oRslts,aCtrls){
for(var strRtn=aCtrls[0];aCtrls.length>0;strRtn=aCtrls.pop()){
if(oRslts[strRtn] == true)
return strRtn;}
return "";
},
initialize: function(dialog)
{
var dlgInit =
{
};
this.loadDefaults(dialog);
dialog.load({"date":this.strName});
dlgInit[this.strGRP1] = true;
dialog.load(dlgInit);
},
commit: function(dialog)
{
var oRslt = dialog.store();
var results = dialog.store();
var data = dialog.store();
this.strName = data["date"];
testValue = results["txt2"];
this.strGRP1 = this.GetRadioSel(oRslt,["Rad1","Rad2"]);
this.strName2 = data["txt4"];
var elements = dialog.store()["txt2"];
for(var i in elements) {
if ( elements[i] > 0 ) {
testValue = elements[i];
}
}
},
loadDefaults: function (dialog) {
dialog.load({
txt2:
{
"*Please make selection": +1,
"Donations": -2,
"Office Supplies": -3,
"Outsourced Services": -4,
"Paper": -5,
"Telecommunications": -6,
"Utilities": -7
}
})
},
description:
{
name: "Billing",
elements:
[
{
type: "view",
elements:
[
{
type: "view",
width: 142,
height: 28,
char_height: 10,
align_children: "align_row",
alignment: "align_left",
elements:
[
{
type: "static_text",
item_id: "stat",
name: "Date:",
width: 35,
height: 18,
char_width: 15,
alignment: "align_top",
font: "dialog",
},
{
type: "edit_text",
item_id: "date",
char_width: 8,
},
{
type: "static_text",
item_id: "sta1",
name: "GL Code:",
width: 55,
height: 17,
alignment: "align_top",
},
{
type: "popup",
item_id: "txt2",
width: 122,
height: 23,
char_width: 8,
},
]
},
{
type: "view",
width: 174,
height: 48,
char_width: 8,
char_height: 8,
align_children: "align_row",
alignment: "align_left",
elements:
[
{
type: "static_text",
item_id: "sta1",
name: "Outsourced:",
width: 70,
height: 16,
alignment: "align_left",
},
{
type: "radio",
item_id: "Rad1",
group_id: "GRP1",
name: "Yes",
width: 30,
height: 23,
},
{
type: "radio",
item_id: "Rad2",
group_id: "GRP1",
name: "No",
width: 30,
height: 23,
},

{
type: "static_text",
item_id: "sta2",
name: "Invoice # Billed On:",
width: 110,
height: 16,
},
{
type: "edit_text",
item_id: "txt4",
char_width: 8,
},
]
},
{
type: "gap",
item_id: "gap1",
width: 149,
height: 3,
char_width: 4,
char_height: 4,
},
{
type: "view",
width: 50,
height: 43,
char_width: 8,
char_height: 8,
align_children: "align_center",
alignment: "align_center",
elements:
[
{
type: "ok",
},
]
},
]
},
]
}
};

if(event.source.forReal && (event.source.stampName == "#Billing"))
{
Dialog.strName = util.printd("mm/dd/yyyy",new Date());
if ("ok" == app.execDialog(Dialog))
{
this.getField("date").value = Dialog.strName;

var textValue;
if (testValue==1) textValue = "Not Specified";
else if (testValue==2) textValue = "Donations";
else if (testValue==3) textValue = "Office Supplies";
else if (testValue==4) textValue = "Outsourced Services";
else if (testValue==5) textValue = "Paper";
else if (testValue==6) textValue = "Telecommunications";
else if (testValue==7) textValue = "Utilities";
this.getField("GLCode").value = textValue;

var strGRP1;
if (Dialog.strGRP1=="Rad1") Dialog.strGRP1 = "Yes";
else if (Dialog.strGRP1=="Rad2") Dialog.strGRP1 = "No";
else if (Dialog.strGRP1=="") Dialog.strGRP1 = "Not Specified";
this.getField("Outsourced").value = Dialog.strGRP1;

this.getField("datebilled").value = Dialog.strName2;
}
}