jQuery验证插件:valid()不适用于远程验证吗? [英] jQuery validation plugin: valid() does not work with remote validation?

查看:93
本文介绍了jQuery验证插件:valid()不适用于远程验证吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从遵循这真棒教程开始,但想在键盘上进行验证,然后将我的错误放在其他地方.远程验证会在适当的时间显示其自身的错误消息,这让我觉得我可以正常工作了.但是,如果我专门问一个带有远程验证的字段是否有效,它说不,实际上,它不是.

I got started by following this awesome tutorial, but wanted to do the validation on on keyup and place my errors somewhere else. The remote validation shows its own error message at the appropriate times, making me think I had it working. But if I ask specifically if a field with remote validation is valid, it says no, actually, its not.

在application.js中,我有这个...

In application.js I have this...

    $("#new_user").validate({
    rules: {
        "user[login]": {required: true, minlength: 3, remote: "/live_validations/check_login"},
    },
    messages: {
        "user[login]": {required: " ", minlength: " ", remote: " "},
    }
});

$("#user_login").keyup(function(){
    if($(this).valid()){
        $(this).siblings(".feedback").html("0");
    }else{
        $(this).siblings(".feedback").html("1");
    }
})

然后在Rails应用中...

And then this in the rails app...

def check_login
    @user = User.find_by_login(params[:user][:login])
    respond_to do |format|
      format.json { render :json => @user ? "false" : "true" }    
    end
  end

我认为我的问题可能与jQuery上的这张票有关,并且试图实现该代码,但是,对于jQuery来说,这有点麻烦.当我说一点时,我是说路.

I think that my problem might have everything to do with this ticket over at jQuery, and tried to implement that code, but, being new to jQuery, it's all a bit over my head. When I say bit, I mean way way.

任何修复它的想法,或者一种新的研究方法,都会有很大的帮助.

Any ideas to fix it, or a new way to look at it, would be a big help.

推荐答案

调用$(this).valid()时,它最终以异步方式调用/live_validations/check_login.不过,valid()方法会立即返回,而不是等待check_login完成,并且由于它不知道结果是什么,因此只会返回false.

When you call $(this).valid(), it ends up calling /live_validations/check_login asynchronously. The valid() method comes back right away though, not waiting for check_login to finish, and since it doesn't know what the result is going to be, it just returns false.

看看Validation插件代码,我认为您应该改为调用element()方法,该方法将返回true,false或undefined-在异步调用运行时它将返回undefined,而valid()仅返回对或错.当您不确定时,您知道您仍然必须等待对服务器的调用返回.

Looking at the Validation plugin code, I think you should call the element() method instead, which will return true, false, or undefined -- it will return undefined while the asynchronous call is running, whereas valid() only returns true or false. When you get undefined, you know you still have to wait for the call to your server to come back.

我认为您可以做这样的事情(未经测试,但可以玩).请注意,对element()的重复调用不会启动新的Ajax调用.只有在值更改时,它们才会这样做.因此,那里没有害处.

I think you can do something like this (untested, but something to play with). Note that the repeated calls to element() will not launch new Ajax calls; they only do so if the value has changed. So there's no harm there.

function checkResult() {
   var valid = $(formSelector).validate().element('#user_login'); // Will not launch a new request if the value hasn't changed
   if (typeof valid == 'undefined') {
     setTimeout("checkResult()", 100);
   }
   else { // it's done!
     if(valid){
      $(this).siblings(".feedback").html("0");
     }else{
      $(this).siblings(".feedback").html("1");
     }
   }
};
$("#user_login").keyup(function(){
  $(formSelector).validate().element('#user_login'); // launches the remote request
  setTimeout("checkResult()", 100);
})

这篇关于jQuery验证插件:valid()不适用于远程验证吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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