OOP: Problem with edit form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chromis
    New Member
    • Jan 2008
    • 113

    OOP: Problem with edit form

    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?actio n=add.
    On submission of the form, index.cfm?actio n=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=up date&id=1.

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

    My code is as follows:

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

    Chromis
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Originally posted by chromis
    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=up date&id=1.

    However what I'm struggling with is how to do that from a form submit on the index.cfm?actio n=edit page.
    Do you mean how to pass an ID?

    Comment

    Working...