Question

I'm looking for the best way to structure form handling in a Coldfusion8 site I inherited.

There are a number of forms which relate to certain interactions, such as "user interactions" (register/login/logout/update). I have set up a global form/validation handler along this example, which works ok.

I have changed the setup a little and since I'm in my first months of working with Coldfusion I'd like to know, if the following way of handling things makes sense:

In my application.cfc I'm declaring session defaults for all "user interaction forms" like so:

<cffunction name="onSessionStart" returnType="boolean" output="false" hint="session initalizer">
    ...
    Session.FormData.User = {
        username="", 
        password="", 
        iln="", 
        companyname="",
        address="",
        ...
        formsubmitted=""
        }
</cffunction>

The idea was to setup defaults for all form fields, so I'm always submitting a "full form", no matter whether the user simply logs in or registers.

Then inside my user-form-handler.cfc I'm declaring validation criteria:

 <cfcomponent extends="controllers.form_switch" output="false" hint="Utility for handling all user-related forms">     
    <cfscript>
        <!--- declare validation methods --->
        VARIABLES.Instance.Validation = {
            username = "string|len_6", 
            password="string|len_6",
            iln="spec_iln",
            ...
            companyname="string",
            address="string",
            formsubmitted="pass"
            }
        <!--- grab default values from Session --->
        VARIABLES.Instance.FormData = SESSION.FormData.User;
     </cfscript> 

     <!--- SET FORM DATA --->
     <cffunction name="SetFormData" access="public" returntype="void" output="false">
        <cfargument name="FormData" type="struct" required="true" hint="Form data" />
            <!--- overwrite default values with values passed in form submisson. --->
            <cfset VARIABLES.Instance.FormData = ARGUMENTS.FormData />
        <cfreturn />
    </cffunction>

    <!--- PROCESS --->
    <cffunction name="Process" access="remote" returntype="struct" output="false" hint="Process">

        <cfset var LOCAL = {} />
        <cfset LOCAL.Response = { Success = true, Errors = [], Data = "" } />
        <!--- call setFormData to overwrite defaults with submitted values --->
        <cfif IsStruct( ARGUMENTS[ 1 ] )>
            <cfset THIS.SetFormData( ARGUMENTS[ 1 ] ) />
        <cfelse>                
            <cfset THIS.SetFormData( ARGUMENTS ) />             
        </cfif> 
        ... validation/commit
        <cfreturn LOCAL.Response />
    </cffunction>

Whenever I submit a user related form, I'm submitting to the PROCESS function inside user-form-handler.cfc, which overwrites the session default values set inside SETFORMDATA and then validates the form fields before commiting to the database and returning the response object.

My Question:
While this works, I'm not sure about declaring default values for all form inputs inside onSessionstart inside my Application.cfc. Is there a better way to do this, when I want to run all user related form submits through this central handler?

Was it helpful?

Solution

If you aren't changing any of the form submission defaults anywhere along the line (such as setting a form variable indicating the landing page along with google search terms or anything else that would change those variables to be something other than blank) you can just accept the blank fields and/or declare the defaults in the processing .cfc. I usually have a function that "pads out" missing fields and sets defaults. Then a separate function that validates the data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top