Search code examples
asp.net-mvcasp.net-mvc-3unobtrusive-validationfluentvalidationasp.net-mvc-validation

ASP.net MVC Validation Hook


I have the following view in ASP.net MVC 3:

@model Models.CreateProjectViewModel

<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>

@using( Html.BeginForm() ) {
    @Html.TextBoxFor(m => m.ProjectName)
    @Html.ValidationMessageFor(m => m.ProjectName)

    <p>
        <input type="submit" value="Save" />
    </p>
}

I am using unobtrusive javascript with jQuery and the Fluent Validation framework.

When I click the Save button and validation fails, is there some event I can hook into to call some custom javascript?

function validationFailed() {
    // do something here only if validation failed
}

How would I tie into the validation so that when it failed (and only if it failed) I could call my validationFailed() function.


Solution

  • You can use the invalidHandler, I believe that is in jquery validation.

    invalidHandler Callback
    Callback for custom code when an invalid form is submitted. Called with a event object as the first argument, and the validator as the second.