Skip to main content
Inspiring
February 2, 2009
Question

CFFORM and CFC usage

  • February 2, 2009
  • 3 replies
  • 447 views
I have form that is calling itself for submission, and at the top of the
form, I have:

<cfif structkeyexists(url,"m")>
<cfswitch expression="#m#">
<cfcase value="0">
<cfinvoke component="compoents.post" method="infoMail">
<cfinvokeargument name="fName" value="#form.fName#">
<cfinvokeargument name="lName" value="#form.lName#">
<cfinvokeargument name="Address1" value="#form.Address1#">
<cfinvokeargument name="Address2" value="#form.Address2#">
<cfinvokeargument name="City" value="#form.City#">
<cfinvokeargument name="State" value="#form.State#">
<cfinvokeargument name="Zip" value="#form.Zip#">
<cfinvokeargument name="Plus4" value="#form.Plus4#">
<cfinvokeargument name="Email" value="#form.Email#">
<cfinvokeargument name="Info" value="#form.Info#">
<cfinvokeargument name="Events" value="#form.Events#">
<cfinvokeargument name="Class" value="#form.Class#">
<cfinvokeargument name="News" value="#form.News#">
</cfinvoke>
</cfcase>
<cfcase value="1">

</cfcase>
</cfswitch>
</cfif>

The problem is that if a checkbox on the form isn't selected, then I get:

Element EVENTS is undefined in FORM

How can I get around this?? On the CFC I have it setting a default
value of 0, and it isn't required.....
    This topic has been closed for replies.

    3 replies

    Inspiring
    February 2, 2009
    You are checking for the existence of a url variable and then trying to process form varialbles.
    Inspiring
    February 2, 2009
    > The problem is that if a checkbox on the form isn't selected, then I get:
    > Element EVENTS is undefined in FORM

    That has nothing to do with your cfc. You cannot use an variable that is not defined. The usual solutions are to set a default with cfparam or include the variable conditionally:

    <cfif structKeyExists(form, "event")>
    do something
    <cfelse>
    do something else
    </cfif>

    Or just pass in all of the arguments at once using "argumentCollection":

    <cfinvoke .... argumentCollection="#form#" />

    Inspiring
    February 2, 2009
    Steve G wrote:
    >
    > The problem is that if a checkbox on the form isn't selected, then I get:
    >
    > Element EVENTS is undefined in FORM
    >
    > How can I get around this?? On the CFC I have it setting a default
    > value of 0, and it isn't required.....

    That is a standard problem with the HTTP standard and the check box and
    radio button controls. If they are not selected no key is sent to the
    server. Your default in the CFC is not working because the code is
    failing when trying to use the non-existent 'form.events' value in the
    <cfinvokeargument...> tag. You just have to code your form handler page
    to understand this might be so. The two most common solutions to this
    is to check for the existence of the field OR to default it to some value.

    The struckKeyExists() function works well to test for the existence of
    form fields as the form and url variables are structures.
    I.E.
    <cfif structKeyExists(form,"Events")>
    <cfinvokeargument name="Events" value="#form.Events#">
    </cfif>

    The <cfparam...> tag is designed to provide a default value to something
    if it does not exists.
    I.E.
    <!--- at the top of the form action page --->
    <cfparam name="form.events" default="0">