Persisting Values in ColdBox
I have never found a way to validate data I feel is 100% right for me, but I think I am getting close, let me explain.
I have a simple ColdBox 'view' containing my user form. This form is used to both create and update user's credentials.
2
3<cfoutput>
4 <fieldset>
5 <div class="fm-req">
6 <label for="firstname">First Name</label>
7 <input id="firstname" name="firstname" type="text" class="text" value="#rc.bean.getfirstname()#" maxlength="15">
8 </div>
9 <div class="fm-req">
10 <label for="surname">Last Name</label>
11 <input id="surname" name="surname" type="text" class="text" maxlength="15" value="#rc.bean.getsurname()#">
12 </div>
13 <div class="fm-req">
14 <label for="email">Email Address</label>
15 <input id="email" name="email" type="text" class="text" maxlength="100" value="#rc.bean.getemail()#">
16 </div>
17 <div class="fm-req">
18 <label for="username">Username</label>
19 <input id="username" name="username" type="text" class="text" maxlength="12" value="#rc.bean.getusername()#">
20 </div>
21 <div class="fm-req">
22 <label for="password">Password</label>
23 <input id="password" name="password" type="password" class="text" maxlength="20" value="#variables.PasswordEncrypt#">
24 </div>
25 </fieldset>
26</cfoutput>
2
3<!--- Edit User --->
4<cffunction name="editUser" access="public" returntype="void" output="false">
5<cfargument name="Event" type="coldbox.system.beans.requestContext">
6 <cfscript>
7 var rc = event.getCollection();// RC Reference
8 var userBean = variables.adminUsersService.createAdminUserBean(); //Create adminUserBean
9 getPlugin('beanFactory').populateBean(userBean);//the magic bean machine
10 variables.adminUsersService.getUserByID(userBean);
11 rc.bean = userBean;
12 event.setValue("pageTitle","Edit System User");//Form H1 Title
13 event.setValue("formURL","/users/updateUser?adminId=#rc.adminId#");
14 event.setValue("buttonValue1","Update User");//H1 Title
15 runEvent(event='users.createUserTabs',private=true); // Create User Tabs
16 Event.setView("user/edit"); // Set the View To Display, after Logic
17 </cfscript>
18</cffunction>
I use one handler to create a new user and another to update, the only difference is the update handler pre populates the object from the database. So far simple stuff. Next I needed to validate my input. Again simple, I would submit to an intermediate handler that would validate my data against the object. However my dilemma is what to do if validation fails!
2<cffunction name="validation" access="public" returntype="void" output="false">
3<cfargument name="Event" type="coldbox.system.beans.requestContext">
4 <cfscript>
5 var rc = event.getCollection();// RC Reference
6 var userBean = variables.adminUsersService.createAdminUserBean(); //Create adminUserBean
7 getPlugin('beanFactory').populateBean(userBean);//the magic bean machine
8
9 errors = userBean.validateUserALL();//Check For Validation Errors
10
11 if NOTT ArrayLen(errors)){//No Validation Errors
12
13 if ( variables.adminUsersService.updateUser(userBean)) {//Update Was OK
14 getPlugin("messagebox").setMessage("info", "User was successfully updated.");
15 }
16 else {//Could Not Update User Show Error Message
17 getPlugin("messagebox").setMessage("error", "Sorry, for some reason this user was not updated!");
18 }
19
20 setNextEvent('users.overView'); // Set the Event To Run, After Logic
21 }//End Of No Validation Errors
22
23 else {//We Have Validation Errors Show The User A Message
24 getPlugin("messagebox").setMessage("error", "<b>The Following Validation Errors Occurred:</b><br />",errors);
25
26 ///what to do here?
27
28 }
29 </cfscript>
30</cffunction>
Option 1: Do I include the 'view' again within the validation handler so the object data would persist?
Option 2: Do I take them back to the original handler that made the post?
Now option 2 would mean my objects data would not persist, it would either be repopulated by the update handler or cleared by the create handler!
However I favour option 2 because by going back to the original handler you don't see in the URL the name of the validation handler as it only an intermediate stage.
So how do you persist the entire object so the user does not lose the data they have posted?
Well ColdBox has an internal flash memory that you can use in order to persist variables across requests without sending them via the URL. – direct quote. I could do the following I guess...
2getPlugin("messagebox").setMessage("error", "<b>The Following Validation Errors Occurred:</b><br />",errors);
3myStruct = {FIRSTNAME=userBean.getFIRSTNAME()}; //etc.....
4persistVariables(varStruct=myStruct);
5setNextEvent(event='users.newUser');

However this gets cumbersome and I have to add variable or create a strut.
So Far I have not found a method which is easier have you?
Sep19



19/09/09 22:59
One may also persist in this way:
setNextEvent
event="myEvent",persist="list,of,variables,in,request,collection