7

I have a form that, when submitted, shows a busy animation and disables the submit button.

Anyone know how to query Microsoft's Sys.Mvc.FormValidation to see if the form passed its test so I can prevent the busy animation showing if the form hasn't actually been submitted? Is there some other work-around?

At present my client side JavaScript looks like this:

$('form').submit(function() {
    $('input[type=submit]', this).attr('disabled', 'disabled');
    ShowBusy();
});

Cheers, Gavin

0

3 Answers 3

11

I gave up in the end and plugged in the JQuery validation library instead using the Microsoft ASP.NET MVC 2 RC and the MicrosoftMvcJQueryValidation.js library file from the "futures" project to automatically wire up everything for me.

This article explains the process: http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx

So with JQuery validation in effect all I needed to do was make a call to valid()...

$('form').submit(function() {
    if ($('form').valid()) {
        $('input[type=submit]', this).attr('disabled', 'disabled');
        ShowBusy();
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Hi SteveCI, not for the context I want to use validation in - see original question ;)
0

Use http://jquery.malsup.com/form/. Here's how I do - this method handles the error (and other conditions) to stop animation. It can also either ajaxify form or submit it right away (depending on the submit flag), and do couple of other things.

function GenericAjaxForm(form, success, submit, beforeSubmit) {
   var form = $(form);
   var options = {
      beforeSubmit: function(data, frm, options) {
         form.disable(true);
         form.find("input:submit:last").after(
            "<span class='ajaxwait'><img src='../Content/images/smallwait.gif' /></span>");
         if (beforeSubmit)
            return beforeSubmit(data, frm, options);
      },
      error: function(xhr, status, error) {
         $(".validation-summary-errors").html(getAjaxErrorMessage(status, xhr));
         form.disable(false);
         form.find(".ajaxwait").remove();
      },
      success: function(data) {
         form.disable(false);
         form.find(".ajaxwait").remove();
         $(".validation-summary-errors").html('');
         if (CheckValidationErrorResponse(data, form))
            return;
         success(data);
      }
   };
   if (submit)
      form.ajaxSubmit(options);
   else
      form.ajaxForm(options);
}

1 Comment

Thanks for that, however I'm trying to plug in to Microsoft's own client-side MVC validation library rather than right my own CheckValidationErrorResponse function. I'm using the automated validation provided when using Data Annotations. I'm hoping there's a nice simple way to query the validation result directly from the Sys.Mvc.FormValidation client-side library, but haven't found anything yet. May have to look at swapping over to JQuery validation if no luck. Came across a "futures" project from Microsoft that does automated validation against JQuery's validation library instead.
0

This only works for MVC 1.0, in MVC 2.0 Microsoft switched to using jQuery and this no longer works.

onformSubmit: function (e) {
    if (!Sys.Mvc.FormContext && Sys.Mvc.FormContext.getValidationForForm(this).validate('submit').length) {
    return;
    showBusy();
}

If you're using the Ajax.BeginForm helper you can do something like

Ajax.BeginForm("action","controller", AjaxOptions{ OnBegin="onformSubmit",...}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.