Reputation: 7533
From a best-practice or performance standpoint which option is best to use for testing if a FORM value is not blank?
<cfif NOT isNull(FORM.Forename)>
OR
<cfif Len(Trim(FORM.Forename)) GT 0>
OR
<cfif FORM.Forename NEQ "">
I don't want the value to be valid if it has something silly like 4 blank spaces in it. I guess 4 blank spaces is not technically a NULL value?
Upvotes: 1
Views: 3079
Reputation: 21
The problem with all of these examples is that the variable needs to exist or it errors (hence Kobby's "isdefined").
To get around missing FORM variables, attributes, etc. (and not have to isDefined test for them all of the time) I constantly use these custom "NULL" functions...
/* "NULL" functions are to assist with setting and returning variable values of the type you want,
whether they already exist or not ... for easy coding.
<cfif NULL("message") EQ "Optional message value" />
#message# now exists <cfif message EQ "Other value" />
<cfif boolNULL("form.optionalVariableName") />
#form.optionalVariableName# now exists as a boolean <cfif form.optionalVariableName />
<cfif valNULL("attributes.expectedNumericVariableName") />
#attributes.expectedNumericVariableName# now exists as a numeric <cfif attributes.expectedNumericVariableName GT 5 />
<cfif lenNULL("query.expectedNonBlankQueryOrStructureVariableName") />
*/
// List not last is all elements except the last
function listNotLast(list) {
// 2nd param is optional list delimiter
var delimiter = arrayLen(arguments) GT 1 ? arguments[2] : ",";
// If more than 1 element return the list without the last one
return listLen(list, delimiter) GT 1 ? listDeleteAt(list, listLen(list, delimiter), delimiter) : list;
}
// Sets and returns a string even if not defined (sets the variable to "" or a query as query.column[1] = "").
function NULL(v) {
// Override the default blank string with optional 2nd param
var NA = arrayLen(arguments) GT 1 ? arguments[2] : "";
// Variable v does not exist. We'll make it an empty string
if (!isDefined(v)) {
// Query vars are treated differently. Can fail if we're a struct. It looks like we want a query variable query.column_name
if (listLen(v, ".") GT 1 AND !listFindNoCase("caller,attributes,request,client,session,variables,cookie,cgi", listFirst(v, "."))) {
// If the query doesn't exist at all make a blank one (NA)
if (!isDefined(listFirst(v, "."))) {
setVariable(listFirst(v, "."), queryNew("NULL"));
}
// Now add the column name we want as a blank array (NA)
queryAddColumn(evaluate(listFirst(v, ".")), listLast(v, "."), [ NA ]);
}
// Non-query variables created here as a blank string (NA)
else setVariable(v, NA);
}
// Originally defined or not we just return our string value
return evaluate(v);
}
// Returns a numeric value even if not defined (sets the variable to 0). Override the set and return in second var. Uses the NULL and listNotLast functions.
function valNULL(v) {
if (!isDefined("NULL") OR !isDefined("listNotLast")) throw(type = "Missing function", message = "The NULL and listNotLast functions are required for the valNULL function.");
// Return a zero if non defined or non-numeric. Override here with 2nd param
var NA = arrayLen(arguments) GT 1 ? arguments[2] : 0;
var LE = listNotLast(v, ".")
// Use the NULL routine to ensure we're already created, test numericy
if (!isNumeric(NULL(v))) {
// We're non-numeric, check if its a query var (NULL will have created if need be)
if (isDefined(LE) AND isQuery(evaluate(LE))) {
// The query may need a row
if (!evaluate(LE & ".recordCount")) queryAddRow(evaluate(LE), 1);
// NULL already returned us a blank query with our column name if it didn't exist, just set our cell's NA (0) value here
querySetCell(evaluate(LE), listLast(v, "."), NA, 1);
// Non-query variables just set NA (0) value
} else setVariable(v, NA);
}
// Originally defined or not we just return our numeric value
return evaluate(v);
}
// Returns a boolean value even if not defined (sets the variable to false). Uses the NULL function. No overrides.
function boolNULL(v) {
if (!isDefined("NULL")) throw(type = "Missing function", message = "The NULL function is required for the boolNULL function.");
// Use the NULL function to return true for "1" values
if (NULL(v) EQ 1) setVariable(v, true);
// If its not boolean then its false. eg. 0, [blank], false, 'fasle' all set and return false
if (NOT isBoolean(NULL(v))) setVariable(v, false);
// Boolean only values returned here
return evaluate(v);
}
// Returns a string length even if not defined (sets the variable to "" and returns 0 length). Uses the NULL function. No overrides.
function lenNULL(v) {
if (!isDefined("NULL")) throw(type = "Missing function", message = "The NULL function is required for the lenNULL function.");
return len(NULL(v));
}
Upvotes: 1
Reputation: 2539
The second
<cfif Len(Trim(FORM.Forename)) GT 0>
The first will not be null. Cf will receive an empty string or no form element. The third is covered by the second.
You may need to wrap the form element with an isdefined depending on the form element type.
Upvotes: 3