Discussion:
cfparam storing integer rather than string
(too old to reply)
whatchamakeofit
2009-07-24 18:27:58 UTC
Permalink
Hi. I have a textbox (not a textarea) that the user should fill out
with an integer value. On the processing page, it takes that value
and inserts it into the db. It's an optional field, and I don't want
to force the user to type in a 0 if the field isn't applicable. So, I
first tried using <cfparam name="form.numChildren" default="0">, but
when I try to output form.numChildren, it comes out as an empty
string, not zero. I've also tried <cfparam name="form.numChildren"
default="0" type="numeric">, but I end up getting the following error:

Invalid parameter type.
The value cannot be converted to a numeric because it is not a simple
value.Simple values are booleans, numbers, strings, and date-time
values.

The reason I want form.numChildren to come in as the integer 0 and not
the string "0" is because when I insert into the db, I don't see the
point in storing it as a string if it's simply a numeric value.

This seems like a very basic problem, but I can't seem to get it
working the way I want it to work. Please help! Thanks
DennBen
2009-07-24 19:14:42 UTC
Permalink
Hi.  I have a textbox (not a textarea) that the user should fill out
with an integer value.  On the processing page, it takes that value
and inserts it into the db.  It's an optional field, and I don't want
to force the user to type in a 0 if the field isn't applicable.  So, I
first tried using <cfparam name="form.numChildren" default="0">, but
when I try to output form.numChildren, it comes out as an empty
string, not zero.  I've also tried <cfparam name="form.numChildren"
Invalid parameter type.
The value cannot be converted to a numeric because it is not a simple
value.Simple values are booleans, numbers, strings, and date-time
values.
The reason I want form.numChildren to come in as the integer 0 and not
the string "0" is because when I insert into the db, I don't see the
point in storing it as a string if it's simply a numeric value.
This seems like a very basic problem, but I can't seem to get it
working the way I want it to work.  Please help!  Thanks
I dont think you are setting the form field to 0 in the <CFPARAM tag,
if you were, it would display as 0 not blank.
put this onthe page you are submitting <CFDUMP var="#FORM#"> and look
to see what form variables were posted, and what the values are. This
should help you find the bug.
whatchamakeofit
2009-07-24 19:27:29 UTC
Permalink
if i use <cfparam name="form.numChildren" default="0">, it displays an
empty string because cfparam tests for existence of a variable. If
the user left it blank, the form variable does exist; it just exists
as an empty string. so, cfparam won't bother changing it to 0.

the only way I can see to get around this is if i use:

<cfif form.numChildren eq "">
<cfset form.numChildren = 0>
</cfif>

I have 8 different form variables where I need to apply this so 8x3=24
lines of code. I would expect cfparam to be the equivalent of the
<cfif...>, but apparently not.
Nick Voss
2009-07-25 13:24:58 UTC
Permalink
Post by whatchamakeofit
if i use <cfparam name="form.numChildren" default="0">, it displays an
empty string because cfparam tests for existence of a variable.  If
the user left it blank, the form variable does exist; it just exists
as an empty string.  so, cfparam won't bother changing it to 0.
<cfif form.numChildren eq "">
        <cfset form.numChildren = 0>
</cfif>
I have 8 different form variables where I need to apply this so 8x3=24
lines of code.  I would expect cfparam to be the equivalent of the
<cfif...>, but apparently not.
You're correct, cfparam just sets a default if the variable doesnt
exist and in this case it does exist with a value of an empty string.

If you want to have the form variable arrive with a 0 then set your
default value on the form itself as a 0. Then your param default (in
the instance that the variable doesnt exist -- like in a multiple
select box or radio/checkbox that wasn't selected) matches your user
interface default.

If you're trying to get away with text boxes to handle numeric entry
and don't want to supply it a default value then your if statement for
data validation is about the only way to go. Of note though is that
you'll probably need to do more validation on your data than you're
doing with just that if. You'll also want to say if not isnumeric
then display an error or change it to zero as well -- keep in mind
with a text field you may get a user who tries to type "two" instead
of 2.

Loading...