简体   繁体   English

Rails,AJAX表单验证

[英]Rails, AJAX form validation

I am new to jQuery, AJAX, and javascript in general, and I am trying to do some front end validation for a rails form that uses an ajax call to have the server run a query. 我是jQuery,AJAX和javascript的新手,我正在尝试对使用ajax调用使服务器运行查询的rails表单进行一些前端验证。 It seems to work well, if I am in the debugger (since the AJAX call has enough time to return and stop the submission of the form), but it wont work all of the time when I am just testing it without the debugger. 如果我在调试器中,这似乎很好用(因为AJAX调用有足够的时间返回并停止提交表单),但是当我在没有调试器的情况下对其进行测试时,它不会一直工作。 I believe this happens because after the ajax call, the javascript validation method exits, and the form submits. 我相信这是因为ajax调用之后,javascript验证方法退出了,并且表单提交了。 Here is my javascript method 这是我的javascript方法

  $('#runner-job-submit').click(function(e) {
    runnerJobValidation(e);
  });

  function runnerJobValidation(e) {
    var user_login = $('#user_login').val();
    var name = $('#name').val();
    var location = $('#location').val();
    var description = $('#description').val();
    var user_exists;
    //AJAX call to check if user exists in the database
    $.post("/jobs/user_exists", {user_login: user_login}, function (response) {
      if(response == "false") {
        $('#runner-new-job-errors').html('');
        e.preventDefault();
        $('#runner-new-job-errors').append('<small style="color:red"> User doesnt exist </small>');
      }
    });
    if(user_login == "" || name == "" || location == "" || description == "") {
        $('#runner-new-job-errors').html('');
        e.preventDefault();
        $('#runner-new-job-errors').append('<small style="color:red"> Please fill out all fields </small>');
      }

  }

And here is my controller action url it is hitting 这是我的控制器动作网址

      def user_exists?
        user_login = params[:user_login]
        user = User.where("login = ?", user_login).first
        user_exists = user.present?
        respond_to do |format|
          format.js  { render :json => user_exists.to_json}
          format.html { redirect_to jobs_path }
        end
  end

Can someone please tell me the correct way to do this. 有人可以告诉我正确的方法吗? I feel the solution is to somehow prevent runnerJobValidation() from exiting before response is received from the server, but I just dont know how to do this. 我认为解决方案是以某种方式阻止runnerJobValidation()在从服务器收到response之前退出,但是我只是不知道该怎么做。

You are trying to make an synchronous ajax request. 您正在尝试发出同步ajax请求。 In short, you can do it by set async in ajax object to false. 简而言之,您可以通过将ajax对象中的async设置为false来实现。

You can found your solution here : How to make JQuery-AJAX request synchronous . 您可以在这里找到您的解决方案: 如何使JQuery-AJAX请求同步

Hope this help. 希望对您有所帮助。

You can user jQuery validation plugin like http://jqueryvalidation.org/ 您可以使用jQuery验证插件,例如http://jqueryvalidation.org/。

For validation you should add new validation method http://jqueryvalidation.org/jQuery.validator.addMethod/ 为了进行验证,您应该添加新的验证方法http://jqueryvalidation.org/jQuery.validator.addMethod/

Something like this: 像这样:

jQuery.validator.addMethod("user_exists", function(value, element, param) {
    var isSuccess = false;
    isSuccess = $.ajax(type: "POST", url: "/jobs/user_exists", data: {user_login: user_login}, dataType: "json").responseText == "false" ? false : true;
    return isSuccess;
}, jQuery.format("User already exists!"));

And for form submit: 并提交表格:

 $("#myform").validate({
  submitHandler: function(form) {
    // some other code
    // maybe disabling submit button
    // then:
    $(form).submit();
  }
 });

About how to properly use plugin read docs at http://validation.bassistance.de/documentation/ 有关如何正确使用插件的信息,请参见http://validation.bassistance.de/documentation/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM