justacoder
justacoder

Reputation: 2704

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.

Upvotes: 0

Views: 631

Answers (2)

Henry
Henry

Reputation: 32915

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

Upvotes: 2

Jason Dean
Jason Dean

Reputation: 9615

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.

Upvotes: 0

Related Questions