CFPARAM and How to Uncheck a Checkbox

For anyone new to application development, you may be perplexed when you create your form for editing some data that should allow a user to uncheck a checkbox and update a record to a status of not checked. You will sooner or later discover an oddity about HTML forms. HTML forms won't allow you to uncheck like you think you should be able to. If you uncheck the checkbox, the value becomes NULL and the form doesn't bother to pass the field. You will find that ColdFusion does have a simple solution and much more with the CFPARAM tag.



CFPARAM primary function is to provide a default value if one isn't provided from a requesting form, a URL, or from some other expected variable like a session ID. It also provides a nice way to check the passed values datatype which comes in handy when you need to assure that a numeric value, date or other datatype is passed.

Setting Defaults without CFPARAM
You can set a default without using CFPARAM by using something like:

<CFIF NOT IsDefined("MyVar")
<cfset myvar="">
</cfif>

This sets a MyVar to have a NULL value. It now exists and has a NULL value.

Checking Datatypes without CFPARAM
Checking the data before passing it into a template is often prefered. This can be accomplished with client side validation using JavaScript that you code or that ColdFusion creates with CFFORM and CFINPUT. Alternatively you can use ColdFusion function like isNumeric() in the processing template and handle datatype problems with your own conditional code

Setting Defaults with CFPARAM
To set a default variable with CFPARAM use the following format:

<CFPARAM NAME="MyVar" DEFAULT="">

If MyVar is not passed to the page it will be created with this tag. In this case it will exist with a NULL value.

Checking Datatypes with CFPARAM
A secondary function of CFPARAM is datatype checking. This makes checking that the proper data type is passed to the template but it will produce an error if the wrong type is passed. The may be OK for some in-house applications or your own adminstration areas but for a public site, client side checking with CFFORM, CFINPUT and Javascript is a better pracice.

Datatypes that can be checked with CFPARAM

  • any
  • array
  • binary
  • boolean
  • date
  • numeric
  • query
  • string
  • struct
  • UUID
  • variablename

Summary

CFPARAM is a great tool in setting defaults and dealing with form fields that are passed with no value. It also helps in maintaining your data integrity by checking datatypes



More CFNewbie?com Articles

 

CFNewbie?com