Check if variable exists in Model-Glue

636 Views Asked by At

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.

2

There are 2 best solutions below

3
On BEST ANSWER

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.

0
On

ValueExists(name:string)

Description:

Does a value of the given name exist in the viewstate?

Returns:

Boolean

Arguments:

Name (required) - The name of the value to check

http://docs.model-glue.com/wiki/ReferenceMaterials/EventApi#ValueExistsname:string