467,920 Members | 1,227 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,920 developers. It's quick & easy.

OOP: Problem with edit form

100+
Hi,

I'm having trouble fully implementing the edit section of a contact admin system, so far I have written the following:

- Bean (Contact.cfc)
- Data Access object (ContactDAO.cfc)
- Gateway (ContactGateway.cfc)

- index.cfm - Deals with the business logic
- display/form.cfm - Produces the form for both add and edit behaviour
- display/process.cfm - Deals with form data
- display/edit.cfm - Displays listing of contacts and allows user to select one to edit.

The add contact part is working and is accessed like this index.cfm?action=add.
On submission of the form, index.cfm?action=process is passed the form values and the data is then sent to the database.
The add page is also supposed to double up as an update / edit page, so that if an id is passed in then the corresponding contact will be retrieved from the database and displayed in the form. This would be in the form of index?action=update&id=1.

However what I'm struggling with is how to do that from a form submit on the index.cfm?action=edit page. I've probably got this structure wrong and need some pointers in the right direction.

My code is as follows:

index.cfm:
Expand|Select|Wrap|Line Numbers
  1. <cfset Request.Ses.User.protect("staff","../index.cfm") />
  2. <cfimport taglib="../../taglib" prefix="func">
  3. <cfinclude template="../../taglib/QueryStrings.cfm" />
  4. <cfparam name="url.action" default="add" />
  5. <cfparam name="url.id" type="string" default="" />
  6.  
  7. <cfswitch expression="#url.action#">
  8.     <cfcase value="add"> 
  9.         <cfinclude template="display/form.cfm">
  10.     </cfcase>
  11.     <cfcase value="edit">
  12.         <cfinclude template="display/edit.cfm">
  13.     </cfcase>
  14.     <cfcase value="delete">
  15.         <cfinclude template="display/delete.cfm">
  16.     </cfcase>    
  17.     <cfcase value="update">
  18.         <cfinclude template="display/form.cfm">
  19.     </cfcase>    
  20.     <cfcase value="process">
  21.         <cfinclude template="display/process.cfm">
  22.     </cfcase>
  23. </cfswitch>
  24.  
  25. <cfinclude template="../../taglib/pagefooter.cfm">
  26.  
display/form.cfm:
Expand|Select|Wrap|Line Numbers
  1. <cfimport taglib="../../taglib" prefix="func">
  2. <cfimport taglib="../../../taglib" prefix="func">
  3.  
  4.  
  5.         <!--- get instance of PersonDAO --->
  6.             <cfset contactDAO = CreateObject("component", 
  7.                                 "com.dataobjects.ContactDAO").init(Request.App.dsn) />
  8.  
  9.             <!--- if there isn't already a person in the request scope, get the person --->
  10.             <cfif Not StructKeyExists(request, "contact")>
  11.                 <cfset request.contact = CreateObject("component", 
  12.                                         "com.beans.Contact").init(url.id) />
  13.                 <cfset contactDAO.read(request.contact) />
  14.             </cfif> 
  15.  
  16.             <!--- if we have an id in either the URL or the person, we're doing an update --->
  17.             <cfif url.id NEQ "" OR request.contact.getId() NEQ "">
  18.                 <cfset action = "Update" />
  19.             </cfif>
  20.  
  21.             <!--- <cfif isDefined("url.action") AND url.action EQ "update" OR isDefined("url.action") AND StructKeyExists(request, "errors") EQ "true"> --->
  22.  
  23.             <h2>Add Contact Item</h2>
  24.  
  25.             <!--- output message if it exists --->
  26.             <cfif StructKeyExists(request, "message")>
  27.                 <cfoutput><p style="color:red;font-weight:bold;">#request.message#</p></cfoutput>
  28.             </cfif>
  29.  
  30.             <!--- output errors if they exist --->
  31.             <cfif StructKeyExists(request, "errors")>
  32.                 <p style="color:red;"><strong>The following errors occurred:</strong></p>
  33.  
  34.                 <cfloop collection="#request.errors#" item="error">
  35.                     <ul>
  36.                         <cfoutput><li>#request.errors[error]#</li></cfoutput>
  37.                     </ul>
  38.                 </cfloop>
  39.             </cfif>
  40.  
  41.             <!--- output form --->
  42.             <cfoutput>
  43.             <form action="index.cfm?action=process" method="post">
  44.                 <input type="hidden" name="id" id="id" size="36" required="no" value="#request.contact.getId()#" />            
  45.                 <p>First Name *</p>
  46.                 <input class="aswide" type="text" maxlength="64" name="firstName" id="firstName" value="#request.contact.getFirstName()#" />
  47.                 <p>Last Name *</p>
  48.                 <input class="aswide" type="text" maxlength="64" name="lastName" id="lastName" value="#request.contact.getLastName()#" />
  49.                 <p>Address *</p>                
  50.                 <textarea class="cms" type="textarea" cols="40" rows="5" name="address" id="address">#request.contact.getAddress()#</textarea>
  51.                 <p>Email Address *</p>
  52.                 <input class="cms" type="text" maxlength="320" name="email" id="email" value="#request.contact.getEmail()#"/>
  53.                 <p>Tel No. *</p>
  54.                 <input class="cms" type="text" maxlength="16" name="telNo" id="telNo" value="#request.contact.getTelNo()#">                
  55.                 <div class="submitBox"><input class="cms" type="submit" value="#action# Contact"></div>
  56.                 <input type="hidden" id="action" name="action" value="#action#" />
  57.             </form>        
  58.             </cfoutput>
  59.  
display/process.cfm:
Expand|Select|Wrap|Line Numbers
  1. <cfscript>
  2.     // initialize variables
  3.     results = StructNew();
  4.     errors = StructNew();
  5.  
  6.     // get instance of DAO
  7.     contactDAO = CreateObject("component", 
  8.                 "com.dataobjects.ContactDAO").init(Request.App.dsn);
  9.  
  10.     // initialize person object with form data
  11.     contact = CreateObject("component", 
  12.                 "com.beans.Contact").init(form.id, form.firstName, 
  13.                                                         form.lastName, form.address, form.email, 
  14.                                                         form.telNo);
  15.  
  16.     // validate contact data using contact bean's validate method
  17.     errors = contact.validate();
  18.  
  19.     // if no errors, take action based on value of action
  20.     if (StructIsEmpty(errors)) {
  21.         switch(form.action) {
  22.             case "Add":
  23.                 // set person id
  24.                 contact.setID(CreateUUID());
  25.  
  26.                 // call create method of DAO
  27.                 results = contactDAO.create(contact);
  28.                 break;
  29.  
  30.             case "Update":
  31.                 // call update method of DAO
  32.                 results = contactDAO.update(contact);
  33.                 break;
  34.  
  35.             case "Delete":
  36.                 // call delete method of DAO
  37.                 results = contactDAO.delete(contact);
  38.                 break;
  39.         }
  40.     } else {
  41.         // errors, so put struct and object in request scope and route back to form
  42.         request.errors = errors;
  43.     }
  44.  
  45.     // if there's a message, put it in the request scope
  46.     if (StructKeyExists(results, "message")) {
  47.         request.message = results.message;
  48.     }
  49.  
  50.     // if this wasn't a delete, put the person in the request scope
  51.     if (form.action NEQ "Delete") {
  52.         request.contact = contact;
  53.     }
  54.  
  55.     // server-side redirect back to form page
  56.     if(StructIsEmpty(errors)) {
  57.         getPageContext().forward("index.cfm?action=success");
  58.     }
  59.     else {
  60.         getPageContext().forward("index.cfm?action=edit");            
  61.     }
  62. </cfscript>
  63.  
display/edit.cfm:
Expand|Select|Wrap|Line Numbers
  1. <cfimport taglib="../../taglib" prefix="func">
  2. <cfimport taglib="../../../taglib" prefix="func">
  3. <cfparam name="url.item" default="">
  4.  
  5.                 <h2>Edit Contact Item</h2>
  6.                 <p>Select a contact to edit from the list below and press edit.</p>
  7.  
  8.                 <!--- get instance of ContactGateway --->
  9.                 <cfset ContactGateway = CreateObject("component", 
  10.                                     "com.dataobjects.ContactGateway").init(Request.App.dsn) />
  11.                 <!--- get all contacts --->                                
  12.                 <cfset request.allContacts = ContactGateway.getAllContacts() />
  13.  
  14.                 <cfset RowsPerPage = 5>
  15.                 <cfparam name="URL.StartRow" default="1" type="numeric">
  16.                 <cfif isNumeric(URL.StartRow) NEQ "yes">
  17.                     <cfset URL.StartRow = 1>
  18.                 </cfif>
  19.                 <cfset StartRowBack = StartRow - RowsPerPage>
  20.                 <cfset StartRowNext = StartRow + RowsPerPage>
  21.                 <cfset TotalRows = request.allContacts.RecordCount>
  22.                 <cfset EndRow = Min(URL.StartRow + RowsPerPage - 1, TotalRows)>
  23.  
  24.                 <cfoutput><div class="cms_results">Displaying: #StartRow# to #EndRow# of #TotalRows#</div></cfoutput>
  25.                 <cfif TotalRows GT RowsPerPage><div class="cms_results">Pages: <cfinclude template="../../../taglib/pageLinksInc.cfm"></div></cfif>
  26.  
  27.                 <cfif StructKeyExists(request,"allContacts")>         
  28.                 <form action="index.cfm?action=update" method="post" enctype="application/x-www-form-urlencoded">
  29.                     <table class="propertiesList">
  30.                         <tr>
  31.                             <td><p>First Name</p></td>
  32.                             <td><p>Last Name</p></td>
  33.                             <td><p>Address</p></td>
  34.                             <td><p>Email</p></td>
  35.                             <td><p>Tel No.</p></td>
  36.                         </tr>                    
  37.                     <cfoutput>
  38.                         <cfloop query="request.allContacts" StartRow="#URL.StartRow#" EndRow="#EndRow#">
  39.                         <tr <cfif currentRow MOD 2>class="even"</cfif>>
  40.                             <td width="20px"><input type="radio" name="id" id="id" value="#id#"></td>
  41.                             <td><p>#firstName#</p></td>
  42.                             <td><p>#lastName#</p></td>    
  43.                             <td><p>#address#</p></td>
  44.                             <td><p>#email#</p></td>
  45.                             <td><p>#telNo#</p></td>                        
  46.                         </tr>
  47.                         </cfloop>
  48.                     </cfoutput>
  49.                     </table>
  50.                     <cfif TotalRows GT RowsPerPage><div class="cms_results"><cfinclude template="../../../taglib/cmsNextPrevInc.cfm"></div></cfif>    
  51.                     <div class="submitBox"><input class="cms" type="submit" value="Edit"></div>         
  52.                 </form>       
  53.                 </cfif>
  54.  
Thanks,

Chromis
Aug 13 '08 #1
  • viewed: 2899
Share:
1 Reply
acoder
Expert Mod 8TB
The add page is also supposed to double up as an update / edit page, so that if an id is passed in then the corresponding contact will be retrieved from the database and displayed in the form. This would be in the form of index?action=update&id=1.

However what I'm struggling with is how to do that from a form submit on the index.cfm?action=edit page.
Do you mean how to pass an ID?
Aug 14 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

56 posts views Thread by Xah Lee | last post: by
34 posts views Thread by Pmb | last post: by
6 posts views Thread by Ivan Weiss | last post: by
7 posts views Thread by Fred Exley | last post: by
3 posts views Thread by Shawn Ferguson | last post: by
65 posts views Thread by Chris Carlen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.