views:

49

answers:

1

I want to trigger some custom code when the client side errors are updated using ASP.NET MVC2 client side validation. I've tracked down this function which I want to hook into:

Sys.Mvc.FormContext.prototype = {

    // ...

    _displayError: function Sys_Mvc_FormContext$_displayError() {
        if (this._validationSummaryElement) {
            if (this._validationSummaryULElement) {
                Sys.Mvc._validationUtil.removeAllChildren(this._validationSummaryULElement);
                for (var i = 0; i < this._errors.length; i++) {
                    var liElement = document.createElement('li');
                    Sys.Mvc._validationUtil.setInnerText(liElement, this._errors[i]);
                    this._validationSummaryULElement.appendChild(liElement);
                }
            }
            Sys.UI.DomElement.removeCssClass(this._validationSummaryElement, Sys.Mvc.FormContext._validationSummaryValidCss);
            Sys.UI.DomElement.addCssClass(this._validationSummaryElement, Sys.Mvc.FormContext._validationSummaryErrorCss);
        }
    },

    // ...

}    

How can I override this function such that my code can

  • call the original function
  • then do some other work
A: 

Found it.

<script type="text/javascript">
    $(function () {
        var old_displayError = Sys.Mvc.FormContext.prototype._displayError;
        Sys.Mvc.FormContext.prototype._displayError = function () {
            old_displayError.apply(this);
            // do other stuff here
        }
    });
</script>

I'm not used to overriding prototype functions, so I was stumped for a bit.

Chris