最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use jQuery Validation plugin with form-level server-side validation? - Stack Overflow

programmeradmin0浏览0评论

What's the best way to trigger errors on elements for server-side validation errors that come back after the form passes the initial client-side validation?

$("#contact_form").validate({
  submitHandler: function(form) {
    $.ajax({
      type: 'POST',
      dataType: 'json',
      url: '/contact/send',
      data: $(form).serialize(),
      success: function(response) {
        if(response.error) { //server came back with validation issues
          var fields = response.fields;
          for(var i=0, var len = fields.length; i < len; i++) {
            var field_name = fields[i].name;
            var field_error = fields[i].error;

            // TRIGGER ERROR ON AFFECTED ELEMENT

          }
          return false;
        }
        //everything went ok, so let's show a thanks message
        showThanks();
      }
    }
});

I'm thinking something like:

$(form).find("[name='" + field_name + "']").triggerError(field_error);

But I didn't see any api methods for manually triggering errors in that manner.

What's the best way to trigger errors on elements for server-side validation errors that come back after the form passes the initial client-side validation?

$("#contact_form").validate({
  submitHandler: function(form) {
    $.ajax({
      type: 'POST',
      dataType: 'json',
      url: '/contact/send',
      data: $(form).serialize(),
      success: function(response) {
        if(response.error) { //server came back with validation issues
          var fields = response.fields;
          for(var i=0, var len = fields.length; i < len; i++) {
            var field_name = fields[i].name;
            var field_error = fields[i].error;

            // TRIGGER ERROR ON AFFECTED ELEMENT

          }
          return false;
        }
        //everything went ok, so let's show a thanks message
        showThanks();
      }
    }
});

I'm thinking something like:

$(form).find("[name='" + field_name + "']").triggerError(field_error);

But I didn't see any api methods for manually triggering errors in that manner.

Share Improve this question asked Aug 9, 2011 at 17:02 user126715user126715 3,8583 gold badges24 silver badges25 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 18

I think I figured it out from the documentation of Validator/showErrors

var validator = $("#contact_form").validate();
validator.showErrors({"state": "Bad state."});

Make it. Write a plugin that will do whatever you want. Or if you get to complicated, simply write a javascript function to do it and call that.

I would write a plugin that would create a div, fill it with the error text and animate it nicely.

On submit of the form, I would make the target of the form an invisible iframe on the page which would then call a function in the topWindow with it's result.

<iframe id="subject_frame" name="submit_frame" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe> 

then in the page in the iframe call a javascript method in the top window that either redirects on success or displays the errors.

In the iframe

<script language="javascript" type="text/javascript">
   window.top.window.submitComplete("<?php echo $response; ?>");
</script>   

In the top window (as an example)

function uploadComplete( result ){
    $.unblockUI();
    if(result == "OK"){
        $.blockUI({ message: "<span style='color:green;'>File upload successful, request submitted.</span><br/><br/>Redirecting..." }); 
        setTimeout(function() { 
            $.unblockUI({ 
                onUnblock: function(){ window.location='thankyou.php'; } 
            }); 
        }, 2000);

    } else {
        $.blockUI({ message: "<span style='color:red;'>Failed.</span><br/><br/>"+result }); 
        $('.blockOverlay').attr('title','Click to remove').click($.unblockUI);
    }
}
发布评论

评论列表(0)

  1. 暂无评论