问题
Reference is made to prior question here: How do I use jquery validate remote validation to set and return form field values to the form action page?
I am using JQuery Validate to perform remote form validation in our Coldfusion Application.
As remote validation errors are caught and prepared for user notification, we are building a return structure which contains not only the validation error, but a group of form fields for use in the form action page.
As the remote validation error structure is merged with our form structure, our validation display to the user is now breaking.
Here is a snippet of the error text being created:
<cfset form.errorfieldlist = listappend(form.errorfieldlist, "password")>
<cfset form.errormessagelist = listappend(form.errormessagelist, "Your <strong>assigned password</strong> is required to proceed.", form.rs)>
Here's a snippet of the initial error structure being set:
<cfset result_struct = {
errorfieldlist = listtoarray(form.errorfieldlist),
errormessagelist = listtoarray(form.errormessagelist, form.RS)
}>
Here is where we append the form structure to the result structure:
<cfset StructAppend(result_struct, form)>
Here is the return back to the validation plugin:
<cfoutput>#serializeJSON(result_struct)#</cfoutput>
Here is the validation plugin formatting the error return for user display:
var errors = {};
$.each(data.ERRORFIELDLIST, function(i, val) {
eval("errors."+val+"='"+data[i]+"'");
});
var dialog_html = '<ul>';
$.each(data.ERRORMESSAGELIST, function(i, val) {
dialog_html += '<li><span style="color:#cd0a0a;">'+val+'</span></li>';
});
BEFORE we started merging the form structure with the result structure, our error reporting routine worked as expected. Here is a sample dump of the #serializeJSON(result_struct)# of that working return:
while(1);{"ERRORMESSAGELIST":["Please enter a valid <strong>password<\/strong> (hint: it's case sensitive).","Don't forget, if you need help at any time, please don't hesitate to <strong>click the 'Contact Support' button below<\/strong> for an immediate reponse. Thanks!"],"ERRORFIELDLIST":["password"]}
And here is what it looks like AFTER we started merging the structures:
while(1);{"ITEMTHUMB_TITLE":"","ERRORMESSAGELIST":"Please enter a valid <strong>password<\/strong> (hint: it's case sensitive).|Don't forget, if you need help at any time, please don't hesitate to <strong>click the 'Contact Support' button below<\/strong> for an immediate reponse. Thanks!","ITEMIMAGE_BORDER":"","CARTDOWNLOADKEY":"13FF45A0-E628-749F-C6BB79F60DF90E6F","ITEMURL_SIZE":"","ITEMNAME":"","FIELDNAMES":"VERIFIED,DLURL,DLLOCATION,ITEMNAME,ITEMTEASE,ITEMIMAGE,ITEMIMAGE_BORDER,ITEMIMAGE_ALT,ITEMIMAGE_TITLE,ITEMTHUMB,ITEMTHUMB_BORDER,ITEMTHUMB_ALT,ITEMTHUMB_TITLE,ITEMPUBNOTES,ITEMURL_SIZE,FIRST_NAME,EMAIL,PASSWORD,CARTDOWNLOADKEY,SUBMIT_BUTTON","ITEMIMAGE_ALT":"","DLLOCATION":"","ITEMTHUMB_BORDER":"","ITEMTEASE":"","RS":"|","ITEMIMAGE_TITLE":"","ITEMTHUMB_ALT":"","FIRST_NAME":"","ITEMPUBNOTES":"","EMAIL":"bmyers@bmyers.com","VERIFIED":"","ERRORFIELDLIST":"password","PASSWORD":"1asdf1","ITEMIMAGE":"","SUBMIT_BUTTON":"Get Download","DLURL":"","ITEMTHUMB":""}
So, in effect, the error modal that should look like this:
expected behavior
... instead looks like this:
unexpected behavior
Can you tell me what is causing this break to happen as a result of the structure merge?
Thanks.
回答1:
I think the structAppend() is overwriting the values from result_struct. Try changing the call to this:
<cfset StructAppend(result_struct, form, false)>
The false here instructs CF to not overwrite the entries in struct1 with those that were found in struct2: http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_s_15.html
回答2:
If we look at your second JSON response(AFTER the structures were merged), the ERRORMESSAGELIST is no more an array, but just a string. As we know, a string is nothing but an array of characters. That is the reason you are seeing each character in <li> tags. The first JSON response worked because ERRORMESSAGELIST was an array of strings in that.
来源:https://stackoverflow.com/questions/8651565/jquery-validate-remote-return-structure-issue