JQuery验证远程返回结构问题

时间:2011-12-28 03:33:59

标签: jquery coldfusion jquery-validate

此处参考先前的问题:How do I use jquery validate remote validation to set and return form field values to the form action page?

我正在使用JQuery Validate在Coldfusion应用程序中执行远程表单验证。

当捕获远程验证错误并为用户通知做好准备时,我们正在构建一个返回结构,其中不仅包含验证错误,还包含一组表单字段,供表单操作页面使用。

由于远程验证错误结构与我们的表单结构合并,我们对用户的验证显示现在正在中断。

以下是正在创建的错误文本的片段:

<cfset form.errorfieldlist = listappend(form.errorfieldlist, "password")>
<cfset form.errormessagelist = listappend(form.errormessagelist, "Your <strong>assigned password</strong> is required to proceed.", form.rs)>

以下是设置初始错误结构的片段:

<cfset result_struct = {
  errorfieldlist = listtoarray(form.errorfieldlist),
  errormessagelist = listtoarray(form.errormessagelist, form.RS)
}>

这是我们将表单结构附加到结果结构的地方:

<cfset StructAppend(result_struct, form)>

以下是返回验证插件的回复:

<cfoutput>#serializeJSON(result_struct)#</cfoutput>

以下是格式化用户显示错误返回的验证插件:

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>';
 });

在我们开始将表单结构与结果结构合并之前,我们的错误报告例程按预期工作。以下是该工作返回的#serializeJSON(result_struct)#的示例转储:

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"]}

以下是我们开始合并结构后的样子:

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":""}

因此,实际上,错误模式应如下所示:

expected behavior

......而是看起来像这样:

unexpected behavior

你能告诉我结构合并会导致这种中断的原因吗?

感谢。

2 个答案:

答案 0 :(得分:0)

如果我们查看你的第二个JSON响应(在结构合并之后),ERRORMESSAGELIST不再是一个数组,而只是一个字符串。我们知道,字符串只是一个字符数组。这就是您在<li>标签中看到每个字符的原因。第一个JSON响应起作用,因为ERRORMESSAGELIST是一个字符串数组。

答案 1 :(得分:0)

我认为structAppend()正在覆盖result_struct中的值。尝试将呼叫更改为:

<cfset StructAppend(result_struct, form, false)>

这里false指示CF不用struct2中的条目覆盖struct1中的条目:http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_s_15.html