Question

I have a custom unobtrusive validator within my ASP.NET MVC project. I have allowed multiple instances of the attribute on a particular property. Everything appears to render correctly. each instance of the attribute contains another lower-case letter appended to it. So, when it renders, it may be something like data-val-myvalidatora or data-val-myvalidatorb.

On the client side, I'm selecting all inputs that I'd expect the unobtrusive validator to be added to, grabbing the attribute, checking for the extra lower-case letter, than adding a new method and adapter for each instance.

If I run F12, I can see that if I check $.validator.methods, my methods are added. However, if I put in data that should fail and hit the form's submit button, my validator does not validate. My validation method is in a standalone function. If I add a breakpoint, it's not hit.

What can I do to ensure that the methods are wired properly?

My other issue that I've noticed is that I have a dictionary at the attribute level to determine how many times the attribute is added to the HTML input element. However, on every subsequent postback, the dictionary does not reset. My guess is that there's some other method that I should be overriding, but I can't determine which it is.

Was it helpful?

Solution

My issue was that I wrapped the unobtrusive validation in the $(document).ready() anonymous function. Once I removed that, everything worked.

OTHER TIPS

Try to re-enable all validations:

    var form = $("form"); // or other form selector

    $(document).ready(function () {
        form.removeData('validator');
        form.removeData('unobtrusiveValidation');
        $.validator.unobtrusive.parse(form);
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top