coldfusionmodel-glue

Check if variable exists in Model-Glue


How can I determine whether a variable exists in Model-Glue II? I'm passing a checkbox (value = 1) via form submission. This done in Controller.cfc within a method that works already for other variables being submitted.

Test A:

<cfif IsDefined("arguments.event.getValue('foobar')")>

</cfif>

Error: Parameter 1 of function IsDefined, which is now arguments.event.getValue('foobar'), must be a syntactically valid variable name.

Test B (assuming M.G. implicitly creates variable with blank/NULL value):

<cfset foo = arguments.event.getValue('foobar') />
<cfif IsNumeric(foo) AND foo GT 0>
  // Code here
</cfif>

Error: Element FOO is undefined in ARGUMENTS.


Solution

  • According to the MG Docs getValue() should return "any". I'm guessing that means that when something just plain doesn't exist that it returns void.

    However, it does have an optional second param to set a default. So you could do this:

    <cfset foo = arguments.event.getValue('foobar', -1) />
    <cfif IsNumeric(foo) AND foo GT 0>
        // Code here
    </cfif>
    

    If you're on CF9 you could also try using the isNull() function. But I do not know if it would work in this situation.