I have a set of radio buttons that I would like to validate on the server side. First I created structure that has the argument names for the key. Then each name is set to different values that the radio button can have. Here is an example:
<cfargument name="frmhs_rad1" type="string" required="false" default="">
<cfargument name="frmhs_rad2" type="string" required="false" default="">
<cfargument name="frmhs_rad3" type="string" required="false" default="">
<cfset validateRadio = {
frmhs_rad1 = 0,
frmhs_rad1 = 1,
frmhs_rad2 = 0,
frmhs_rad2 = 1,
frmhs_rad2 = 2,
frmhs_rad3 = 0,
frmhs_rad3 = 1,
frmhs_rad3 = 2
}>
Here is an example of one of my cfqueryparam
lines:
<cfqueryparam value="#trim(structKeyExists(validateRadio, arguments.frmhs_rad1) ? validateRadio[arguments.frmhs_rad1]:"")#" cfsqltype="cf_sql_char" null="#yesNoFormat(!len(trim(arguments.frmhs_rad1)))#" />
If I don't check the radio button the value in my Database will remain NULL
. If I check the radio button then I see a blank space in this field. I think that my code should prevent that. If value exists then I check if it's valid in validateRadio
structure. If value doesn't exist then it should be set to NULL
. I'm not sure why my code is failing. Can anyone see the problem with my code?
I think I see part of the problem and hopefully this will get you headed in the right direction. I think you are attempting to build a structure containing all of the possible values for your radio buttons. The problem is that you have more than one possible value for the same name (key). Structures do not support that.
You have this code:
But if you dump the
validateRadio
structure you will see that it only contains the last values that you set for a particular name (key).The other values are lost.
Next, I don't believe your
queryparam
logic will work either. You have this condition:In that code the
arguments.frmhs_rad1
variable will hold the value of the radio button (1, 2, or 3). Those values are not the keys of your structure so they will never be found. The keys of your structure arefrmhs_rad1
,frmhs_rad2
andfrmhs_rad3
.