views:

617

answers:

1

I am using the jQuery Form plugin to submit AJAX requests. I am trying to do some simple validation (make sure fields are filled out) before submitting. I am not trying to run any AJAX validation prior to submission. Before I added beforeSubmit to the options of the ajaxform() method, the form would submit and my controller code would run fine. Once I added the beforeSubmit option with a validation function name, the form will not submit whether I return false, true, or nothing from the validate method. Here is my initial code that sets up the form:

$(document).ready(function() {
var options = {
    beforeSubmit: function(data, form, options) { return validate(data, form, options); },
    url: '/PromoManagement/Images/AddImages?partial=true',
    success: updateImageList
};

    $('#frmAddImages').ajaxForm(options);
});

and here is my validation function:

function validate(data, form, options) {
    var returnMessage = new Array();

    $('#txtImagePath,#txtImageCaption').removeClass('validationError-input');
    clearValidation();

    if ($('#txtImagePath').val().length == 0) {
        returnMessage.push('<li>Image Path is required</li>');
        $('#txtImagePath').addClass('validationError-input');
    }
    else { 
        if (!parseFileExtension($('#txtImagePath').val())) {
            returnMessage.push('<li>Please provide an image with an extension of .jpg, .gif, or .png</li>');
            $('#txtImagePath').addClass('validationError-input');    
        }    
    }

    if (returnMessage.length > 0) {
        $('.errorList').show();
    }

    if (returnMessage.length != 0) {
        buildValidationList(returnMessage);
        return false;
    }
    else {
        return true;
    }

}

I have tried returning nothing if no errors are encountered, but I get the same results. I am able to break into the validation code and see what is being returned from the function, and that is always as expected. Am I missing something here?

+2  A: 

I think that beforeSubmit is not passing the form to the validate function. Try wrapping validate in an anonymous function and supplying the correct form parameter.

EDIT Looks like the beforeSubmit function takes three arguments (API), the second of which is the form. I've updated my example to reflect this. After looking at your code, it appears that you don't use it anyway -- so this has been updated as well. I'm not sure exactly what is going on as it appears that it should be working. Have you confirmed that you don't get any javascript errors in your validation function?

beforeSubmit: function(data,form,options) {
    return validate();
}
tvanfosson
Tried this: I get the same results. The validate() function gets called, returns true, but the form never submits.
Mark Struzinski
I've updated my answer. It looks like you could also just change your validate function to take the three arguments. I'm not sure why it's not working, though. Have you checked for javascript errors?
tvanfosson
I tried adding these arguments, but I'm still getting the same results. I can step through the javascript code with no errors up through the point that I'm sure it's not returning false. Then the function completes but the post never happens and the success callback is not invoked
Mark Struzinski
Got it. After playing with the options and using the data object to do the validation, it worked. I'm not sure why it was failing, though, because it was still returning true if the form validated. Thanks for the tip!
Mark Struzinski