Tuesday, November 21, 2006

Handy user defined functions

Here are a few handy user defined functions that help with traditional form controls. Well, actually it's one udf and two variations on a theme.

They let you pass either a boolean value or two simple values that will be tested for equality.

You just use them in place of the attribute of the corresponding name in things like checkboxes, radio buttons and select controls. I always hated that embedded cfif approach. It always seems to flip out whatever editor you're using because of the nested angle brackets.


<cffunction name="checked">
<cfargument name="v1" />
<cfargument name="v2" />
<cfset var attrib = "" />

<cfif NOT structExists(arguments,"v2")>
<cfif IsBoolean(arguments.v1)>
<cfif arguments.v1>
<cfset attrib = 'checked="true"' />
</cfif>
<cfelse>
<cfthrow message="The parameter passed to the checked udf was not boolean" />
</cfif>
<cfelse>
<cfif arguments.v1 eq arguments.v2>
<cfset attrib = 'checked="true"' />
</cfif>
</cfif>

<cfreturn attrib />
</cffunction>

<cffunction name="selected">
<cfargument name="v1" />
<cfargument name="v2" />
<cfset var attrib = "" />

<cfif NOT structExists(arguments,"v2")>
<cfif IsBoolean(arguments.v1)>
<cfif arguments.v1>
<cfset attrib = 'selected="true"' />
</cfif>
<cfelse>
<cfthrow message="The parameter passed to the selected udf was not boolean" />
</cfif>
<cfelse>
<cfif arguments.v1 eq arguments.v2>
<cfset attrib = 'selected="true"' />
</cfif>
</cfif>

<cfreturn attrib />
</cffunction>

<cffunction name="disabled">
<cfargument name="v1" />
<cfargument name="v2" />
<cfset var attrib = "" />

<cfif NOT structExists(arguments,"v2")>
<cfif IsBoolean(arguments.v1)>
<cfif arguments.v1>
<cfset attrib = 'disabled="true"' />
</cfif>
<cfelse>
<cfthrow message="The parameter passed to the DISABLED udf was not boolean" />
</cfif>
<cfelse>
<cfif arguments.v1 eq arguments.v2>
<cfset attrib = 'disabled="true"' />
</cfif>
</cfif>
<cfreturn attrib />
</cffunction>

No comments:

Post a Comment