JQuery验证远程返回结构问题 [英] JQuery Validate Remote Return Structure Issue

查看:372
本文介绍了JQuery验证远程返回结构问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处参考以前的问题:如何使用jquery validate远程验证来设置表单字段值并将其返回到表单操作页面?



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



由于远程验证错误被捕获并准备好用户通知,我们正在构建一个返回结构,只有验证错误,但是一组表单字段用于表单操作页面。



由于远程验证错误结构与我们的表单结构合并,我们的验证显示



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

 < cfset form.errorfieldlist = listappend(form.errorfieldlist,password)> 
< cfset form.errormessagelist = listappend(form.errormessagelist,您的< strong>指定的密码< / strong>是必需的。

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

 < 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 :[请输入有效的< strong>密码< \ / strong>(提示:大小写敏感),不要忘记,如果您在任何时候需要帮助, ; strong>点击< \ / strong>下方的联系支持按钮,立即回复Thanks!,ERRORFIELDLIST:[password]} 
pre>

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

  while(1); {ITEMTHUMB_TITLE:,ERRORMESSAGELIST:请输入有效的< strong>密码< \ / strong> ,如果您在任何时候需要帮助,请随时点击< strong>点击< \ / strong>下方的联系支持按钮以立即做出回复。,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: } 

因此,实际上,错误模态应该是这样:



预期行为



...,如下所示:



意想不到的行为



你能告诉我结构合并的结果是什么原因导致这种中断?



谢谢。

解决方案

我认为structAppend()覆盖了来自result_struct的值。尝试将调用更改为:

 < cfset StructAppend(result_struct,form,false)> 

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


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.

解决方案

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

这篇关于JQuery验证远程返回结构问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆