Are you using a bean to persist your form's submitted values?

What i do, cuz i'm a little intellectually lazy, like Ray, is to place a validate function in my bean. Others abstract the validation to a separate object, but i prefer having it in one package. It's just a little easier for me to see what's going on. Once reactor is stable, i'll probably move to using that.

If you need an example to work off of, i've pasted one below:

<cfcomponent displayName="Visitor" hint="I'm a Visitor that fills out a contact form" output="false">

    <cfinclude template="../udfs/IsEmail.cfm" />
   
    <cffunction name="init" access="public" returntype="any" output="false">
        <cfargument name="title" type="string" required="false" default="" />   
        <cfargument name="fname" type="string" required="false" default="" />
        <cfargument name="lname" type="string" required="false" default="" />
        <cfargument name="email" type="string" required="false" default="" />
        <cfargument name="phone" type="string" required="false" default="" />
        <cfargument name="bestTime" type="string" required="false" default="" />
        <cfargument name="address1" type="string" required="false" default="" />
        <cfargument name="address2" type="string" required="false" default="" />
        <cfargument name="city" type="string" required="false" default="" />
        <cfargument name="postalCode" type="string" required="false" default="" />
        <cfargument name="country" type="string" required="false" default="" />
        <cfargument name="text" type="string" required="false" default="" />
        <cfargument name="replyLanguage" type="string" required="false" default="" />
        <cfargument name="contactMode" type="string" required="false" default="" />
        <cfargument name="projectId" type="string" required="false" default="" />
        <cfargument name="subscribeMe" type="boolean" required="false" default="true" />
        <cfargument name="contactMe" type="boolean" required="false" default="true" />
       
        <cfargument name="requiredFields" type="string" required="false" default=""
            hint="a comma delimited list of required fields, can be defaulted here" />
        <cfset setInstanceFromStruct(arguments) />
        <cfset variables.requiredFields = arguments.requiredFields />
        <cfset variables.invalidFields = "" />
        <cfset variables.missingFields = "" />
        <cfreturn this />
     </cffunction>
   
       
    <cffunction name="getInstanceAsStruct" access="public" returntype="struct" output="false">
        <cfset var returnData = StructNew() />
        <cfset returnData.title = getTitle() />
        <cfset returnData.fname = getFname() />
        <cfset returnData.lname = getLname() />
        <cfset returnData.email = getEmail() />
        <cfset returnData.phone = getPhone() />
        <cfset returnData.bestTime = getBestTime() />
        <cfset returnData.address1 = getAddress1() />
        <cfset returnData.address2 = getAddress2() />
        <cfset returnData.city = getCity() />
        <cfset returnData.postalCode = getPostalCode() />
        <cfset returnData.country = getCountry() />
        <cfset returnData.text = getText() />
        <cfset returnData.replyLanguage = getReplyLanguage() />
        <cfset returnData.contactMode = getContactMode() />
        <cfset returnData.projectId = getProjectId() />
        <cfset returnData.subscribeMe = getSubscribeMe() />
        <cfset returnData.contactMe = getContactMe() />
        <cfreturn returnData />
    </cffunction>

    <cffunction name="setInstanceFromStruct" access="public" returntype="any" output="false">
        <cfargument name="data" required="true" type="struct" hint="The struct containing the data to set." />
        <cfscript>
   
            if(structKeyExists(data,"title")){
                setTitle(data.title);
                }
            if(structKeyExists(data,"fname")){
                setFname(data.fname);
                }
            if(structKeyExists(data,"lname")){
                setLname(data.lname);
                }
            if(structKeyExists(data,"email")){
                setEmail(data.email);
                }
            if(structKeyExists(data,"phone")){
                setPhone(data.phone);
                }
            if(structKeyExists(data,"bestTime")){
                setBestTime(data.bestTime);
                }
            if(structKeyExists(data,"address1")){
                setAddress1(data.address1);
                }
            if(structKeyExists(data,"address2")){
                setAddress2(data.address2);
                }
            if(structKeyExists(data,"city")){
                setCity(data.city);
                }
            if(structKeyExists(data,"postalCode")){
                setPostalCode(data.postalCode);
                }
            if(structKeyExists(data,"country")){
                setCountry(data.country);
                }
            if(structKeyExists(data,"text")){
                setText(data.text);
                }
            if(structKeyExists(data,"replyLanguage")){
                setReplyLanguage(data.replyLanguage);
                }
            if(structKeyExists(data,"contactMode")){
                setContactMode(data.contactMode);
                }
            if(structKeyExists(data,"projectId")){
                setProjectId(data.projectId);
                }
            if(structKeyExists(data,"subscribeMe")){
                setSubscribeMe(true);
            } else {
                setSubscribeMe(false);
            }
            if(structKeyExists(data,"contactMe")){
                setContactMe(data.contactMe);
            } else {
                setContactMe(false);
            }
        </cfscript>
        <cfreturn this />
    </cffunction>
   
    <cffunction name="validate" access="public" returntype="boolean" output="no">
        <cfset var valid = true />
        <cfset variables.invalidFields = "" />
        <cfset variables.missingFields = "" />
       
        <cfloop list="#variables.requiredFields#" index="key">
            <cfif Len(variables.instance[key]) EQ 0>
                <cfset variables.missingFields = listAppend(variables.missingFields,"#key#") />
                <cfset valid = false>
            </cfif>
        </cfloop>
       
        <cfif Len(getEmail()) AND NOT IsEmail(getEmail())>
             <cfset variables.invalidFields = listAppend(variables.invalidFields,"email") />
            <cfset valid = false />
        </cfif>
               
        <cfreturn valid>
    </cffunction>
   
    <cffunction name="getValidationIndex" access="public" returntype="struct" output="false"
        hint="I return a structure that indicates the validation results for the interface">
        <cfset var key = "" />
        <cfset var val = StructNew() />
       
        <cfscript>   
            val.email='';
            val.displayMissingMessage = false;
            val.displayInvalidMessage = false;
            val.isValid = true;
        </cfscript>

        <cfif Len(getMissingFields())>
            <cfloop index="key" list="#getMissingFields()#">
                <cfset val[key] = "missing"  />
            </cfloop>
            <cfset val.displayMissingMessage = true />
            <cfset val.isValid = false />
        </cfif>
   
        <cfif Len(getInvalidFields())>
            <cfloop index="key" list="#getInvalidFields()#">
                <cfset val[key] = "invalid"  />
            </cfloop>
            <cfset val.displayInvalidMessage = true />
            <cfset val.isValid = false />
        </cfif>
       
        <cfreturn val />
    </cffunction>
   
    <cffunction name="getMissingFields" access="public" returntype="string" output="false">
        <cfreturn variables.missingFields />
    </cffunction>
   
    <cffunction name="getInvalidFields" access="public" returntype="string" output="false">
        <cfreturn variables.invalidFields />
    </cffunction>
   
    <cffunction name="getTitle" access="public" returntype="string" output="false">
        <cfreturn variables.instance.title />
    </cffunction>
    <cffunction name="setTitle" access="private" returntype="VOID" output="false">
        <cfargument name="title" type="string" required="true" />
        <cfset variables.instance.title = arguments.title />
    </cffunction>

    <cffunction name="getFname" access="public" returntype="string" output="false">
        <cfreturn variables.instance.fname />
    </cffunction>
    <cffunction name="setFname" access="private" returntype="VOID" output="false">
        <cfargument name="fname" type="string" required="true" />
        <cfset variables.instance.fname = arguments.fname />
    </cffunction>

    <cffunction name="getLname" access="public" returntype="string" output="false">
        <cfreturn variables.instance.lname />
    </cffunction>
    <cffunction name="setLname" access="private" returntype="VOID" output="false">
        <cfargument name="lname" type="string" required="true" />
        <cfset variables.instance.lname = arguments.lname />
    </cffunction>

    <cffunction name="getEmail" access="public" returntype="string" output="false">
        <cfreturn variables.instance.email />
    </cffunction>
    <cffunction name="setEmail" access="private" returntype="VOID" output="false">
        <cfargument name="email" type="string" required="true" />
        <cfset variables.instance.email = arguments.email />
    </cffunction>

    <cffunction name="getPhone" access="public" returntype="string" output="false">
        <cfreturn variables.instance.phone />
    </cffunction>
    <cffunction name="setPhone" access="private" returntype="VOID" output="false">
        <cfargument name="phone" type="string" required="true" />
        <cfset variables.instance.phone = arguments.phone />
    </cffunction>
   
    <cffunction name="getBestTime" access="public" returntype="string" output="false">
        <cfreturn variables.instance.bestTime />
    </cffunction>
    <cffunction name="setBestTime" access="private" returntype="VOID" output="false">
        <cfargument name="bestTime" type="string" required="true" />
        <cfset variables.instance.bestTime = arguments.bestTime />
    </cffunction>

    <cffunction name="getAddress1" access="public" returntype="string" output="false">
        <cfreturn variables.instance.address1 />
    </cffunction>
    <cffunction name="setAddress1" access="private" returntype="VOID" output="false">
        <cfargument name="address1" type="string" required="true" />
        <cfset variables.instance.address1 = arguments.address1 />
    </cffunction>

    <cffunction name="getAddress2" access="public" returntype="string" output="false">
        <cfreturn variables.instance.address2 />
    </cffunction>
    <cffunction name="setAddress2" access="private" returntype="VOID" output="false">
        <cfargument name="address2" type="string" required="true" />
        <cfset variables.instance.address2 = arguments.address2 />
    </cffunction>
   
    <cffunction name="getCity" access="public" returntype="string" output="false">
        <cfreturn variables.instance.city />
    </cffunction>
    <cffunction name="setCity" access="private" returntype="VOID" output="false">
        <cfargument name="city" type="string" required="true" />
        <cfset variables.instance.city = arguments.city />
    </cffunction>


    <cffunction name="getPostalCode" access="public" returntype="string" output="false">
        <cfreturn variables.instance.postalCode />
    </cffunction>
    <cffunction name="setPostalCode" access="private" returntype="VOID" output="false">
        <cfargument name="postalCode" type="string" required="true" />
        <cfset variables.instance.postalCode = arguments.postalCode />
    </cffunction>


    <cffunction name="getCountry" access="public" returntype="string" output="false">
        <cfreturn variables.instance.country />
    </cffunction>
    <cffunction name="setCountry" access="private" returntype="VOID" output="false">
        <cfargument name="country" type="string" required="true" />
        <cfset variables.instance.country = arguments.country />
    </cffunction>
   
    <cffunction name="getText" access="public" returntype="string" output="false">
        <cfreturn variables.instance.text />
    </cffunction>
    <cffunction name="setText" access="private" returntype="VOID" output="false">
        <cfargument name="text" type="string" required="true" />
        <cfset variables.instance.text = arguments.text />
    </cffunction>
   
    <cffunction name="getReplyLanguage" access="public" returntype="string" output="false">
        <cfreturn variables.instance.replyLanguage />
    </cffunction>
    <cffunction name="setReplyLanguage" access="private" returntype="VOID" output="false">
        <cfargument name="replyLanguage" type="string" required="true" />
        <cfset variables.instance.replyLanguage = arguments.replyLanguage />
    </cffunction>

    <cffunction name="getContactMode" access="public" returntype="string" output="false">
        <cfreturn variables.instance.contactMode />
    </cffunction>
    <cffunction name="setContactMode" access="private" returntype="VOID" output="false">
        <cfargument name="contactMode" type="string" required="true" />
        <cfset variables.instance.contactMode = arguments.contactMode />
    </cffunction>
   
    <cffunction name="getProjectId" access="public" returntype="string" output="false">
        <cfreturn variables.instance.ProjectId />
    </cffunction>
    <cffunction name="setProjectId" access="public" returntype="VOID" output="false">
        <cfargument name="ProjectId" type="string" required="true" />
        <cfset variables.instance.ProjectId = arguments.ProjectId />
    </cffunction>
   
    <cffunction name="getSubscribeMe" access="public" returntype="boolean" output="false">
        <cfreturn variables.instance.subscribeMe />
    </cffunction>
    <cffunction name="setSubscribeMe" access="private" returntype="VOID" output="false">
        <cfargument name="subscribeMe" type="boolean" required="true" />
        <cfset variables.instance.subscribeMe = arguments.subscribeMe />
    </cffunction>
   
    <cffunction name="getContactMe" access="public" returntype="any" output="false">
        <cfreturn variables.instance.contactMe />
    </cffunction>
    <cffunction name="setContactMe" access="private" returntype="VOID" output="false">
        <cfargument name="contactMe" type="any" required="true" />
        <cfset variables.instance.contactMe = arguments.contactMe />
    </cffunction>
   
</cfcomponent>


ciao,
Nando

Jason Daiger wrote:
This isn't CFC specific but I think this list is best suited to help answer my question.  I'm working on an application (built using Fusebox 5) and am using FB5 as the MVC framework.  I am placing as much of the business logic (i.e. Model) into CFC's as possible and keeping the View's 'clean' from any business logic. I say clean b/c a view is never "truly" business logic free. Simple example: email address is required to create a profile in the system (business rule) therefore place a red asterisk next to it on the data entry form.  It's that form validation area I'm looking for some suggestions/guidance.  Where do people place form specific validation, the Model or View?  And by Form specific validation I'm specifically referring to required fields, checking field lengths, email address checks, etc.  
 
Side note, I may have sat through one to many of Ray Camden's classes on security and hence have become paranoid b/c I always check field lengths, required fields, etc on the server side, regardless of what is going on w/ the client side, just in case someone tried to save the HTML page locally, hack it up and see what happens if they repost it.  Also, I understand that some other frameworks, such as Model Glue, encapsulate all form or url parameters into a special object and pass this into a CFC for processing and since FB5 does the same thing and places those items in the attributes scope I could pass that to a CFC for validation.  However, that does not feel like a good idea and hence I'm looking for suggestions & opinions.
 
Thanks,
Jason
 
Jason Daiger
PH: 410.480.8148 x301
 
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to [email protected] with the words 'unsubscribe cfcdev' as the subject of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com).

An archive of the CFCDev list is available at www.mail-archive.com/[email protected]


--


Aria Media Sagl
CP 234
6934 Bioggio
Switzerland
www.aria-media.com


Reply via email to