This is slightly off topic in terms of MVC and the GetPageContext() methodology, but I thought it might be useful anyway, given the “validation” slant we’ve started to take.

 

All of our forms post to themselves, and the start of every form page is the validation routine.  Validation itself is performed by a validation object.  I find keeping the handling logic in the form page itself to be beneficial in terms of encapsulating the function of the page itself.  A typical form in our application looks like this:

 

Someform.cfm:

 

<cfparam name=”form.bSubmitted” type=”numeric” default=”0”/>

 

<!--- This is the form handler //--->

<cfif form.bSubmitted>

            <cfset validator = CreateObject(“component”, “cfc.Validator”)>

 

<cfset validator.CheckComplete(form.field1, “Name”)>

            <cfset validator.CheckDate(form.field2,  “Birthday”)>

 

            <cfif validator.ErrorCount is 0>

                        <!--- Do something with the data here //--->

                        <cfinvoke component=”cfc.SomethingManager” method=”AddSomething”>

                                    <cfinvokeargument name=”name” value=”#form.field1”/>

<cfinvokeargument name=”birthday” value=”#form.field2”/>

                        </cfinvoke>                  

 

                        <!--- Redirect to a confirmation page or something …. //--->

                        <cflocation url="" addtoken=”no”>

            <cfelse>

                        <cfset errorList = validator.GetErrorList()>

            </cfif>

</cfif>

 

 

<!--- This is the form itself //--->

<cfparam name=”form.field1” type=”string” default=””/>

<cfparam name=”form.field2” type=”string” default=””/>

<cfparam name=”errorList” type=”string” default=””/>

 

<!--- Insert HTML stuff here //--->

 

<form name=”someForm” action="" method=”post”>

            <cfoutput>

                        <cfif errorMessage is not “”>

                                    <cfinvoke component=”cfc.ErrorManager” method=”formatErrorsHTML”>

<cfinvokeargument name=”errorList” value=”#errorList#”/>

</cfinvoke>

<br><br>

                        </cfif>

 

                        Name: <input type=”text” name=”field1” value=”#form.field1#”/><br>

Birthday: <input type=”text” name=”field2” value=”#form.field2#”/>

</cfoutput>

 

<input type=”submit” name=”dataAction” value=”Submit”/>

 

            <input type=”hidden” name=”bSubmitted” value=”1”/>

</form>

 

 

 

The “bSubmitted” field is a little trick that lets you know the form as submitted either by a button or by pressing enter on a text field.  We’re also obviously very into encapsulating validation and error handling logic in CFCs.

 

HTH,

Roland

 

 


From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Peter H
Sent: Tuesday, June 14, 2005 7:12 PM
To: [email protected]
Subject: RE: [CFCDev] MVC and GetPageContext.Forward()

 

Hi Anthony,

When I was looking into validation using coldfusion, I found many references to using cfinlcudes but couldn't find an example of it anywhere.

The approach used here came about simply because I couldn't think of a better way to cleanly seperate the form itself from the validation.

Theres bound to be advantages and disadvantages to both so If you have an example of using cfincludes for validation could you post it for me.

Any body else have views on this?

Cheers, Pete (aka lad4bear)




----Original Message Follows----
From: "Anthony Israel-Davis" <[EMAIL PROTECTED]>
Reply-To: [email protected]
To: <[email protected]>
Subject: RE: [CFCDev] MVC and GetPageContext.Forward()
Date: Tue, 14 Jun 2005 12:53:42 -0700

Been lurking for quite some time, but now I'm finally on the board.

Peter, thanks for the example - that helped clarify this thread for me.
However, I need to better understand why getPageContext would be used
here. What is the advantage of using getPageContext over simply
cfincluding the dsp_form.cfm?

Thanks

Anthony


   _____

From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Dawson, Michael
Sent: Tuesday, June 14, 2005 12:13 PM
To: [email protected]
Subject: RE: [CFCDev] MVC and GetPageContext.Forward()

This looks great!  Thanks for the example.

M!ke


   _____

From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
On Behalf Of Peter H
Sent: Tuesday, June 14, 2005 1:26 PM
To: [email protected]
Subject: RE: [CFCDev] MVC and GetPageContext.Forward()
Sorry for the naff (and I do mean naff example) but I'm heading
out for dinner in about 10 mins so had to dash this off. It's not
elegant and would work a lot better is you passed a validation object
back instead and just check for isValid etc
Here's the input form:
<cfscript>
if(StructKeyExists(request, 'Form')) Form = Request.Form;
</cfscript>
<cfparam name="Form.txtFirstName" default="" />
<cfparam name="Form.txtLastName" default="" />
<cfparam name="Form.txtAge" default="" />
<cfparam name="Form.Errors" default="StructNew()" />
<html>
<head>
<title>Person Details</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>
<body>
<form action="" method="post" name="form1"
target="_self">
  <table width="75%" border="1">
    <tr>
      <td>First Name:</td>
      <td><input name="txtFirstName" type="text"
id="txtFirstName" value="<cfoutput>#Form.txtFirstName#</cfoutput>"
size="40" maxlength="50"></td>
   <td>
     <cfif StructCount(Form.Errors) gt 0 and
StructKeyExists(Form.Errors,
'txtFirstName')><cfoutput>#Form.Errors['txtFirstName']#</cfoutput></cfif
>
   </td>
    </tr>
    <tr>
      <td>Last Name:</td>
      <td><input name="txtLastName" type="text" id="txtLastName"
value="<cfoutput>#Form.txtLastName#</cfoutput>" size="40"
maxlength="50"></td>
   <td>
     <cfif StructCount(Form.Errors) gt 0 and
StructKeyExists(Form.Errors,
'txtLastName')><cfoutput>#Form.Errors['txtLastName']#</cfoutput></cfif>
   </td>
    </tr>
    <tr>
      <td>Age:</td>
      <td><input name="txtAge" type="text" id="txtAge"
value="<cfoutput>#Form.txtAge#</cfoutput>" size="40"
maxlength="50"></td>
   <td>
     <cfif StructCount(Form.Errors) gt 0 and
StructKeyExists(Form.Errors,
'txtAge')><cfoutput>#Form.Errors['txtAge']#</cfoutput></cfif>
   </td>
    </tr>
    <tr align="center">
      <td colspan="2"><input type="submit" name="Submit"
value="Submit"></td>
    </tr>
  </table>
</form>
</body>
</html>
Here is the validation form:
<cfscript>
Errors = StructNew();

if(Len(Form.txtFirstName) lt 1)
  StructInsert(Errors, 'txtFirstName', 'You must enter your
first name');
if(Len(Form.txtLastName) lt 1)
  StructInsert(Errors, 'txtLastName', 'You must enter your last
name');

if(not isNumeric(Form.txtAge) or Form.txtAge lt 1 or
Form.txtAge gt 100)
  StructInsert(Errors, 'txtAge', 'You must enter a valid number
between 1 and 100');
if(StructCount(Errors) gt 0)
  Form.Errors = Errors;
</cfscript>
<cfif StructKeyExists(Form, 'Errors') >
<cfscript>
  Request.Form = Form;
  GetPageContext().Forward('dsp.form.cfm');
</cfscript>
<cfelse>
<cfoutput>Congratulation #Form.txtFirstName#
#Form.txtLastName#, you look great for a #Form.txtAge# year
old!</cfoutput>
</cfif>


Cheers, Pete (aka lad4bear)
----------------------------------------------------------
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).

CFCDev is supported by New Atlanta, makers of BlueDragon
http://www.newatlanta.com/products/bluedragon/index.cfm

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



----------------------------------------------------------
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).

CFCDev is supported by New Atlanta, makers of BlueDragon
http://www.newatlanta.com/products/bluedragon/index.cfm

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

----------------------------------------------------------
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).

CFCDev is supported by New Atlanta, makers of BlueDragon
http://www.newatlanta.com/products/bluedragon/index.cfm

An archive of the CFCDev list is available at www.mail-archive.com/[email protected] ----------------------------------------------------------
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).

CFCDev is supported by New Atlanta, makers of BlueDragon
http://www.newatlanta.com/products/bluedragon/index.cfm

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

Reply via email to