ColdBox3.8.0/CF10 - Checking for RC/PRC variables

Hi,

I am trying to check for a variable in the view that may or may not have been defined in the handler. Whether I used “isDefined” or “valueExists”; I still get variable is undefined in RC/PRC. Am I missing something?

`

<cfif #prc.qSystems.RecordCount# gt “0”>

`

isDefined() and valueExists() evaluates a string.

if youre working with dynamic variable names, youll have to eval the variable before using those functions.

Not sure I’m following… i thought using isDefined() is to evaluate whether the variable exists or not.

I see you’ve tried these things:

<cfif isDefined(#rc.searchValue#)>
<cfif not event.valueExists(rc.searchValue)>
<cfif #prc.qSystems.RecordCount# gt “0”>

May I suggest these instead. As a general rule, you should avoid “isDefined()”. To check for your search string and whether you should search or not:

<cfif structKeyExists(rc, ‘searchValue’) AND len(rc.searchValue) GT 0>

<cfif prc.qSystems.RecordCount GT 0>

I’m guessing you might be relatively new to Coldfusion. I mention that because new folks sometimes tend to over use the # signs. Typically, a variable can be referenced without it, unless used as an attribute.

<cfif qQuery.RecordCount GT 0>
vs.

I have been told that even the attribute use doesn’t need it, however, I always feel like I’m violating good HTML standards, even though CFML is not HTML. As you’re learning, try it without quotes and # signs first, just to see what you can get away with!

Also, if you had to build your variable name and didn’t know it. Like maybe you have myVar_1, myVar_2, etc. You can do this:
<cfif structKeyExists(rc, ‘myVar_#counter#’)> (Assuming a counter variable from some loop.)

You can’t pass the actual variable into those function, or of course it will error trying to resolve them. The variable name needs to be quoted as a string.

<cfif isDefined( ‘rc.searchValue’ )>

<cfif not event.valueExists( ‘searchValue’ )>

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Yes CaptainPalapa, I am brandnew to ColdFusion and ColdBox. I’ve been told to master ColdFusion first before diving into ColdBox, but with time constraint, I have to try and learn both. Thank you for all the good tips!

I appreciate everyone’s patience with my elementary questions.