I have a remote CFC that returns a structure. It is called using cfajaxproxy. I want JSON to return in order, that is, first in the structure first in the JSON object. However, the returned JSON is in mixed order.
Here is the remote function.
<cfcomponent displayname="validation" hint=""> <cffunction name="validateForm" displayname="validateForm" hint="" access="remote" verifyClient="yes" returntype="struct"> <cfargument name="formVals" type="struct" required="yes"> <cfset errors = StructNew()> <cfif formVals.project neq "project"> <cfset errors["project"] = "Invalid project name." /> </cfif> <cfif Len(formVals.description) eq 0> <cfset errors["description"] = "Please enter a description." /> </cfif> <cfif StructIsEmpty(errors)> <cfset errors["message"]["type"] = "success"> <cfset errors["message"]["text"] = "Client and server-side validation passed successfully."> <cfset errors["areErrors"] = false> <cfelse> <cfset errors["message"]["type"] = "validation"> <cfset errors["message"]["text"] = "Please fix the errors, and resubmit."> <cfset errors["areErrors"] = true> </cfif> <cfreturn errors /> </cffunction> </cfcomponent>
This is cfajaxproxy, which I installed at the top of my form page.
<cfajaxproxy cfc="validation" jsclassname="validation">
Here's the call made to the remote function in the onSubmit handler of my form.
var v = new validation(); v.setHTTPMethod("POST"); var errors = v.validateForm(o);
Here is the data (o variable above) that the functions are sent to the post request.
{"formVals":{"project":"","description":""}}
Here the JSON response is returned from the function.
{"message":{"text":"Please fix the errors, and resubmit.","type":"validation"},"description":"Please enter a description.","project":"Invalid project name.","areErrors":true}
I want the answer to be in the same order in which the structure was created that would look like this.
{"project":"Invalid project name.","description":"Please enter a description.","message":{"text":"Please fix the errors, and resubmit.","type":"validation"},"areErrors":true}
That way, when I repeat the answer, I can set the focus in the first form field with an error in it.
var focusSet = false; $.each(errors, function(key, val){ //alert(key + ': ' + val); if(key != 'message' && key != 'areErrors') { var fi = $('#' + key).parents('.formItem').filter(':first'); fi.addClass("inError"); fi.find('.err').filter(':first').html(val); if(!focusSet) { $('#' + key).focus(); focusSet = true; } } });
Currently, these places are focused in the second field of the form, description, and not in the project field.