asp.net mvc 3 验证摘要未通过不引人注目的验证显示

发布于 2024-10-20 23:34:55 字数 705 浏览 1 评论 0原文

我在让 asp.net MVC 客户端验证按照我想要的方式工作时遇到问题。

我基本上可以正常工作,但是,在用户单击提交按钮之前,验证摘要不会显示,即使各个输入被突出显示为无效,因为用户在表单中进行了选项卡/单击等操作。这一切都发生在客户端。

我本以为一旦发现输入字段无效,就会显示验证摘要。

这是设计使然吗?有什么办法可以解决这个问题,因为我希望在发现其中一个输入字段无效时立即显示验证摘要。

我的代码基本上是,

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
...
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(false)
        @Html.EditorFor(model => model);   
        ...

我的 _Layout.cshtml 引用 jquery-1.4.4.min.js

I'm having problems getting the asp.net MVC client-side validation to work how I want it.

I have it basically working, however, the validation summary is not displayed until the user clicks the submit button, even though the individual inputs are being highlighted as invalid as the user tabs/clicks etc their way through the form. This is all happening client-side.

I would have thought the that the validation summary would be displayed as soon as an input field was discovered that was invalid.

Is this behaviour by design? Is there any way around it, as I would like the validation summary to be displayed as soon as it is discovered that one of the input fields is invalid.

My code is basically,

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
...
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(false)
        @Html.EditorFor(model => model);   
        ...

And my _Layout.cshtml references jquery-1.4.4.min.js.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难得心□动 2024-10-27 23:34:55

我使用了 Torbjörn Nomells 答案的一个版本,

除了这里,我将 ResetSummary 挂在验证器对象上

$.validator.prototype.resetSummary= function () {
    var form = $(this.currentForm);
    form.find("[data-valmsg-summary=true]")
        .removeClass("validation-summary-errors")
        .addClass("validation-summary-valid")
        .find("ul")
        .empty();
    return this;
};

,然后将其调用更改为

$.validator.setDefaults({
    showErrors: function (errorMap, errorList) {
        this.defaultShowErrors();
        this.checkForm();
        if (this.errorList.length) {
            $(this.currentForm).triggerHandler("invalid-form", [this]);
        } else {
            this.resetSummary();
        }
    } 
});

I used a version of Torbjörn Nomells answer

Except here I hang resetSummary off the validator object

$.validator.prototype.resetSummary= function () {
    var form = $(this.currentForm);
    form.find("[data-valmsg-summary=true]")
        .removeClass("validation-summary-errors")
        .addClass("validation-summary-valid")
        .find("ul")
        .empty();
    return this;
};

Then change calling it to

$.validator.setDefaults({
    showErrors: function (errorMap, errorList) {
        this.defaultShowErrors();
        this.checkForm();
        if (this.errorList.length) {
            $(this.currentForm).triggerHandler("invalid-form", [this]);
        } else {
            this.resetSummary();
        }
    } 
});
不醒的梦 2024-10-27 23:34:55

您可以在 onready 中设置更频繁地触发验证摘要:

var validator = $('form').data('validator');
validator.settings.showErrors = function (map, errors) {
    this.defaultShowErrors();
    this.checkForm();
    if (this.errorList.length)
        $(this.currentForm).triggerHandler("invalid-form", [this]);
    else
        $(this.currentForm).resetSummary();
    }
}

这是上面使用的 ResetSummary:

jQuery.fn.resetSummary = function () {
    var form = this.is('form') ? this : this.closest('form');
    form.find("[data-valmsg-summary=true]")
        .removeClass("validation-summary-errors")
        .addClass("validation-summary-valid")
        .find("ul")
        .empty();
    return this;
};

You can setup the validation summary to be triggered a lot more often, in onready:

var validator = $('form').data('validator');
validator.settings.showErrors = function (map, errors) {
    this.defaultShowErrors();
    this.checkForm();
    if (this.errorList.length)
        $(this.currentForm).triggerHandler("invalid-form", [this]);
    else
        $(this.currentForm).resetSummary();
    }
}

Here's the resetSummary used above:

jQuery.fn.resetSummary = function () {
    var form = this.is('form') ? this : this.closest('form');
    form.find("[data-valmsg-summary=true]")
        .removeClass("validation-summary-errors")
        .addClass("validation-summary-valid")
        .find("ul")
        .empty();
    return this;
};
你与清晨阳光 2024-10-27 23:34:55

我在这里有一个类似的问题: 如何在验证摘要中显示 MVC 3 客户端验证结果,但 Darin 建议的解决方案似乎并没有按照我(可能还有您)想要的方式工作。

I have a similar question open here: How to display MVC 3 client side validation results in validation summary but the suggested solution by Darin does not seem to work the way I (and probably you) want it to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文