开发者

Is there an API for jQuery unobtrusive validation?

I have this validation summary in my code:

@Html.ValidationSummary(false, "Fix Error", new { id = "valSumId" } )

Is there an API so I can add validation error during runtime? Thou开发者_JAVA百科gh the following code works, it feels brittle, there's nothing to stop ASP.NET MVC team from not using ul li tags for displaying errors on the next ASP.NET MVC release.

$('input[type=submit]').click(function (e) {
    e.preventDefault();
    if ($('form').valid()) {
        /* ajax submit here */

        if (anErrorOccured) {
            $('#valSumId').removeClass().addClass('validation-summary-errors');
            errorList = $('#valSumId > ul');

                    // the message is variable, coming from JSON, and can be a list
            errorList.append($('<li />').text('Your ordered quantity is above the stock level'));
        }
    }
});


This is what I wanted to achieve (see the jQuery code at the bottom): http://www.ienablemuch.com/2011/07/ivalidatableobject-client-side.html

For now, while I still don't know the API for inserting the errors to client-side validation, hardcoding them with ul li would be fine. I already made a nice wrapper around what I wanted to achieve, so in any point in the future I discover the jQuery validation API, the program is insulated to the code changes needed be made.


No, there isn't. The approach you outline does not belong on the client side. Messages like "Your ordered quantity is above the stock level" should really come from the server. You can set up validations like that with remote validation or server side validation, both of which would feed into the validation summary without a need to manually add them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜