I have a remote CFC that returns a structure. It is called using cfajaxproxy. I want the JSON returned to be in order i.e. first into the structure first into the JSON object. However, the JSON that is returned is in a mixed up order.
Here's 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 the cfajaxproxy that I have set 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's the data (o variable above) that is sent to the function in the post request.
{"formVals":{"project":"","description":""}}
Here's the JSON response 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 response to be in the same order as the structure was created which 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 iterate over the response I can set the focus to 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;
}
}
});
Right now this places focus in the second field of the form, description, instead of in the project field.