I'm writing a contacts section for a cms on a website, I've decided to write the section in OO code. So far I have my Contacts object and a page structure I would use for a procedural site.
/com/Contacts.cfc
Expand|Select|Wrap|Line Numbers
- <cfcomponent displayname="Contact" output="false" hint="Contact component">
- <!--- properties: used for self-documentation --->
- <cfproperty name="dsn" displayname="dsn" hint="Contact id (UUID)" type="string" required="false" />
- <cfproperty name="id" displayname="id" hint="Contact id (UUID)" type="string" required="false" />
- <cfproperty name="firstName" displayname="firstName" hint="The person's first name"
- type="string" required="false" />
- <cfproperty name="lastName" displayname="lastName" hint="The person's last name" type="string"
- required="false" />
- <cfproperty name="address" displayname="address" hint="The person's last name" type="string"
- required="false" />
- <cfproperty name="email" displayname="email" hint="The person's email address" type="string"
- required="false" />
- <cfproperty name="telNo" displayname="telNo" hint="The person's telephone number" type="string"
- required="false" />
- <!--- pseudo-constructor: sets default values if init method isn't called --->
- <cfscript>
- // datasource
- variables.dsn = "";
- // user details
- variable.id = "";
- variables.firstName = "";
- variables.lastName = "";
- variables.email = "";
- variables.address = "";
- variables.telNo = "";
- </cfscript>
- <cffunction name="init" access="public" output="false" returntype="Contact" hint="Constructor for this CFC">
- <!--- take DSN as argument --->
- <cfargument name="dsn" type="string" required="true" hint="The datasource name" />
- <!--- arguments for the constructor, all of which are optional (no-arg constructor) --->
- <cfargument name="id" displayName="id" type="string" hint="The person's ID (UUID)"
- required="false" default="" />
- <cfargument name="firstName" displayName="firstName" type="string" hint="The contact's first name"
- required="false" default="" />
- <cfargument name="lastName" displayName="lastName" type="string" hint="The contact's last name"
- required="false" default="" />
- <cfargument name="address" displayName="address" type="string" hint="The contact's address"
- required="false" default="" />
- <cfargument name="email" displayName="email" type="string" hint="The contact's email address"
- required="false" default="" />
- <cfargument name="telNo" displayName="telNo" type="string" hint="The contact's phone
- number" required="false" default="" />
- <!--- call the setters for each of the Person attributes and pass in the arguments --->
- <cfscript>
- setId(arguments.id);
- setFirstName(arguments.firstName);
- setLastName(arguments.lastName);
- setAddress(arguments.address);
- setEmail(arguments.email);
- setTelNo(arguments.telNo);
- </cfscript>
- <!--- return this CFC --->
- <cfreturn this />
- </cffunction>
- <!--- getters and setters (aka accessors and mutators) --->
- <cffunction name="getFirstName" access="public" output="false" returntype="string">
- <cfreturn variables.firstName />
- </cffunction>
- <cffunction name="setFirstName" access="public" output="false" returntype="void">
- <cfargument name="firstName" type="string" required="true" />
- <cfset variables.firstName = arguments.firstName />
- </cffunction>
- <cffunction name="getLastName" access="private" output="false" returntype="string">
- <cfreturn variables.lastName />
- </cffunction>
- <cffunction name="setLastName" access="public" output="false" returntype="void">
- <cfargument name="lastName" type="string" required="true" />
- <cfset variables.lastName = arguments.lastName />
- </cffunction>
- <cffunction name="getEmail" access="public" output="false" returntype="string">
- <cfreturn variables.email />
- </cffunction>
- <cffunction name="setEmail" access="public" output="false" returntype="void">
- <cfargument name="email" type="string" required="true" />
- <cfset variables.email = arguments.email />
- </cffunction>
- <cffunction name="getAddress" access="public" output="true" returntype="string">
- <cfreturn variables.address />
- </cffunction>
- <cffunction name="setAddress" access="public" output="false" returntype="void">
- <cfargument name="address" type="string" required="true" />
- <cfset variables.address = arguments.address />
- </cffunction>
- <cffunction name="getTelno" access="public" output="true" returntype="string">
- <cfreturn variables.telNo />
- </cffunction>
- <cffunction name="setTelno" access="public" output="false" returntype="void">
- <cfargument name="telNo" type="string" required="true" />
- <cfset variables.telNo = arguments.telNo />
- </cffunction>
- <cffunction name="getID" access="public" output="true" returntype="string">
- <cfreturn variables.ID />
- </cffunction>
- <cffunction name="setID" access="public" output="false" returntype="void">
- <cfargument name="id" type="string" required="true" />
- <cfset variables.id = arguments.id />
- </cffunction>
- <!--- CRUD - Persistent data functions --->
- <cffunction name="create" access="public" output="false" returntype="boolean" hint="CRUD Method">
- <cfargument name="contact" type="com.Contact" required="true" />
- <cftransaction>
- <cfquery name="qCreate" datasource="#variables.dsn#">
- INSERT INTO
- contacts
- (
- firstName,
- lastName,
- address,
- email,
- telNo
- )
- VALUES
- (
- <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.car.getFirstname()#" />,
- <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.car.getLastname()#" />,
- <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.car.getAddress()#" />,
- <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.car.getEmail()#" />,
- <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.car.getTelno()#" />
- )
- SELECT @@IDENTITY AS newID
- </cfquery>
- <cfset arguments.contact.setID(qCreate.newID) />
- </cftransaction>
- </cffunction>
- <cffunction name="read" access="public" returntype="Void" output="false" hint="CRUD method">
- <cfargument name="contact" type="com.Contact" required="true" />
- <cfargument name="contactID" type="String" required="yes" />
- <cfset var qRead = 0 />
- <cfquery name="qRead" datasource="#variables.dsn#">
- SELECT
- firstName,
- lastName,
- address,
- email,
- telNo
- FROM
- contacts
- WHERE
- id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.contactID#" />
- </cfquery>
- <cfif qRead.RecordCount>
- <cfset arguments.contact.setID(arguments.contactID) />
- <cfset arguments.contact.setFirstname(qRead.firstname) />
- <cfset arguments.contact.setLastname(qRead.lastname) />
- <cfset arguments.contact.setAddress(qRead.address) />
- <cfset arguments.contact.setEmail(qRead.email) />
- <cfset arguments.contact.setTelno(qRead.telno) />
- <cfelse>
- <cfthrow type="emptyRecordset" errorcode="Contact.read.emptyRecordset" message="Contact with ContactID #arguments.contactID# not found" />
- </cfif>
- </cffunction>
- <cffunction name="update" access="public" returntype="Void" output="false" hint="CRUD method">
- <cfargument name="contact" type="com.Contact" required="yes" />
- <cfquery name="qUpdate" datasource="#variables.dsn#">
- UPDATE
- contacts
- SET
- firstName = <cfqueryparam cfsqltype="cf_sql_varchar" value="#contact.getFirstname()#" />,
- lastName = <cfqueryparam cfsqltype="cf_sql_varchar" value="#contact.getLastname()#" />,
- address = <cfqueryparam cfsqltype="cf_sql_varchar" value="#contact.getAddress()#" />,
- email = <cfqueryparam cfsqltype="cf_sql_varchar" value="#contact.getEmail()#" />,
- telNo = <cfqueryparam cfsqltype="cf_sql_varchar" value="#contact.getTelno()#" />
- WHERE
- id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.contact.getID()#" />
- </cfquery>
- </cffunction>
- <cffunction name="delete" access="public" returntype="Void" output="false" hint="CRUD method">
- <cfargument name="contact" type="com.Contact" required="yes" />
- <cfquery name="qDeletePresenter" datasource="#variables.dsn#">
- DELETE FROM
- contacts
- WHERE
- id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.contact.getID()#" />
- ;
- </cfquery>
- </cffunction>
- <!--- commit methods --->
- <cffunction name="commit" access="public" returntype="Void" output="false" hint="commit method">
- <cfargument name="car" type="com.Contact" required="yes" />
- <cfquery name="qExists" datasource="#variables.dsn#">
- SELECT
- id
- FROM
- contacts
- WHERE
- id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.contact.getId()#" />
- ;
- </cfquery>
- <cfif qExists.recordcount>
- <cfset this.update(arguments.contact) />
- <cfelse>
- <cfset this.create(arguments.contact) />
- </cfif>
- </cffunction>
- <!--- public methods --->
- <cffunction name="validate" access="public" returntype="Struct" output="false" hint="I validate all properties in the Contact Object">
- <cfscript>
- // initialize variables
- var errors = StructNew();
- // validate values
- if (getFirstName() EQ "") {
- errors.firstName = "The first name field is required";
- }
- if (getLastName() EQ "") {
- errors.lastName = "The last name field is required";
- }
- if (getAddress() EQ "") {
- errors.address = "The address field is required";
- }
- if (getTelno() EQ "") {
- errors.telNo = "The telephone number field is required";
- }
- // if we got an email address, validate the format
- if (getEmail() NEQ "") {
- if (Not isEmail(getEmail())) {
- errors.email = "Your email address is not valid. Please re-enter.";
- }
- }
- else {
- errors.email = "The email field is required.";
- }
- return errors;
- </cfscript>
- </cffunction>
- <!--- function to validate email --->
- <cffunction name="isEmail" access="public" output="false" returntype="boolean" hint="Validates
- the format of the string passed in to see if it's a valid email address">
- <cfargument name="email" type="string" required="true" />
- <cfscript>
- if (REFindNoCase("^['_a-z0-9-]+(\.['_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.(([a-z]{2,3})|(aero|coop|info|museum|name))$",
- arguments.email)) {
- return true;
- } else {
- return false;
- }
- </cfscript>
- </cffunction>
- </cfcomponent>
index.cfm - Contains case statements for url.action
display/add.cfm
display/edit.cfm
display/delete.cfm
com/contacts.cfc
Then I have the usual header and footer includes.
What I'm struggling with is how to structure my pages and deal with the validation of form data. The following is a structure I've kind of cobbled together, based on the idea that there should only be one form for adding / editing (creating/updating) and a cfm for processing the data sent from the form.
index.cfm - Contains case statements for url.action
display/form.cfm - Displays form for creating, updating of contacts.
display/create.cfm - Creates contact object from form elements and adds contact to database
display/update.cfm - Updates contact object from form elements
Has anyone got any examples of how they structure their OO form pages? I'm at the beginning of the CFOO curve and a bit confused!
Thanks,
Chromis