7

I have been working with the jQuery form plugin and the jQuery Validation plugin:

Form: http://jquery.malsup.com/form/ Validation: http://bassistance.de/jquery-plugins/jquery-plugin-validation/

I am trying to work with both of them to play nice toegther in terms of validate document, THEN submit using the jQuery form plugin.

Any ideas?

2 Answers 2

10

from the docs:

$(".selector").validate({
   submitHandler: function(form) {
    $(form).ajaxSubmit();
   }
})
Sign up to request clarification or add additional context in comments.

2 Comments

Alas the docs are bunkum. The above code does a standard form submit, but sets the content type to XMLHttpRequest - if you look at what's being submitted with this it's otherwise identical to what you'd get from a standard submit. I'm trying to work out how to fix this now and will post back when I work out the solution. ref stackoverflow.com/questions/18713714/…
My solution FYI is to modify my tests and accept the reality that the jQuery Forms plugin sends the form data as a URL Encoded param string, not as a stringified JSON hash.
2

According to http://malsup.com/jquery/form/#validation you can use the beforeSubmit option to provide a callback that returns true or false.

In this callback simply use the .valid() plugin method described here http://jqueryvalidation.org/valid

$(document).ready(function() {
    // jQuery Validation Plugin
    $("#myForm1").validate();

    // jQuery form Plugin
    var options = { 
        target:        '#output1',   // target element(s) to be updated with server     response 
        beforeSubmit:  validate,     // pre-submit callback 
        success:       showResponse  // post-submit callback 
    };

    // bind form using 'ajaxForm' 
    $('#myForm1').ajaxForm(options); 
});

// Return validation status from jQuery validate plugin.
function validate(formData, jqForm, options) { 
    return $('#myForm1').valid();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.