OOP: Problem implementing a add, edit, delete Contact form | Member | | Join Date: Jan 2008
Posts: 113
| |
Hi,
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 -
<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>
-
I usually structure my pages like this:
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
| | Member | | Join Date: Jan 2008
Posts: 113
| | | re: OOP: Problem implementing a add, edit, delete Contact form
Ok, I can't figure out how to delete this post so I'll post a new one about where I've got up to.
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: OOP: Problem implementing a add, edit, delete Contact form Quote:
Originally Posted by chromis What I'm struggling with is how to structure my pages and deal with the validation of form data. The problem with validation in COOP (Coldfusion OOP) is that you have two answers that you don't like to hear: "it depends" and "there's no one right answer". Here's an old link, but it gives you an idea of what I mean. This link may help a little. You may also want to look at the series of blog posts on form validation on bennadel.com.
| | Member | | Join Date: Jan 2008
Posts: 113
| | | re: OOP: Problem implementing a add, edit, delete Contact form
Hi acoder,
Thanks for the links I'll have a look into these. I was going to code the website I'm creating in a OO fashion, though it's taking too long to work out the best way of doing things. I guess I'll have to keep this as a side project until I'm confident enough with it.
Thanks.
Chromis
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: OOP: Problem implementing a add, edit, delete Contact form
If you've never programmed in OO before, it can be a bit of a steep learning curve, but I'm sure you'll get there. If you're trying to stick to MVC as well, it can be even more difficult at first, though it'll be beneficial in the long run. Only use MVC for medium to large projects; there's no need for the complexity for small projects.
| | Member | | Join Date: Jan 2008
Posts: 113
| | | re: OOP: Problem implementing a add, edit, delete Contact form
Ok cool I'll remember that. What I struggle with is how to structure the pages for the smaller projects, is the structure I use above ok or do i need to create controller objects for dealing with page requests such as index.cfm?action=edit?
Any chance you could give me an example of a small project you have worked on so I can see how you do things?
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: OOP: Problem implementing a add, edit, delete Contact form
You could use a DAO bean. This is a good series of articles, in particular you may find this part useful.
| | Member | | Join Date: Jan 2008
Posts: 113
| | | re: OOP: Problem implementing a add, edit, delete Contact form
Hi acoder,
Thanks I'd actually been reading through that article. I'm building a good list of resources though now, ben nadel and sean corfield have good blogs that i've been looking through. It's just going to take some time i guess.
Thanks,
Chromis
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: OOP: Problem implementing a add, edit, delete Contact form
Yes, those are good blogs as well as Raymond Camden's and a few others.
Anyway, good luck on the OOP quest! :)
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|