ASP.net MVC 验证挂钩

发布于 2024-12-08 06:51:47 字数 832 浏览 3 评论 0原文

我在 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>
}

我将不引人注目的 javascript 与 jQuery 和 Fluent Validation 框架一起使用。

当我单击“保存”按钮并且验证失败时,是否有一些事件我可以挂钩来调用一些自定义 JavaScript?

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

我如何与验证联系起来,以便当它失败时(并且仅当它失败时)我可以调用我的validationFailed()函数。

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.

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

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

发布评论

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

评论(2

凯凯我们等你回来 2024-12-15 06:51:47

正如 jQuery/Validation 文档所述,您可以使用 invalidHandler 在提交无效表单时做出反应。

但由于 MVC 不显眼的验证会实例化验证本身,因此您必须稍后挂钩此事件。

使用与 jQuery/Validation 完全相同的机制:您可以将代码绑定到表单元素自定义事件“invalid-form.validate”,该事件对应于 invalidHandler!

$(document).ready(function() {
    $('#myform').bind('invalid-form.validate',function(){
        alert('invalid form!');
    });
});

要为您的表单提供 id,请使用:

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myform" })) {
} 

更新:

获取现有验证对象的另一种方法是在表单上调用 validate()。如果此表单的验证器已创建,它将被返回:

编辑:初始化发生后,更改.settings.invalidHandler对于绑定来说太晚了。因此,除非您重新初始化验证器,否则以下代码片段将不再起作用。

$(document).ready(function() {
    var existingValdiation = $("#myform").validate();
    //the settings property corresponds to the options parameter
    existingValdiation.settings.invalidHandler = function (form) {
        alert('invalid form!');
    };
    // not recommended:
    // re-init would bind settings.invalidHandler to 'invalid-form.validate'
    // existingValdiation.init();
});

As jQuery/Validation docs says, you could use invalidHandler to react when an invalid form is submitted.

But since MVC unobtrusive validation instantiates the Validation itself, you have to hook this event later.

Using exactly the same mechanism as the jQuery/Validation does: you could bind your code to the form elements custom event 'invalid-form.validate', witch corresponds to the invalidHandler!

$(document).ready(function() {
    $('#myform').bind('invalid-form.validate',function(){
        alert('invalid form!');
    });
});

to give your form an id use:

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myform" })) {
} 

Update:

An alternative way to get the existing validation object is calling validate() on the form. If a validator for this form was already created, it will be returned:

Edit: after initialization occured, changing .settings.invalidHandler is too late for binding. So the following snippet will not work [anymore] unless you re-init the validator.

$(document).ready(function() {
    var existingValdiation = $("#myform").validate();
    //the settings property corresponds to the options parameter
    existingValdiation.settings.invalidHandler = function (form) {
        alert('invalid form!');
    };
    // not recommended:
    // re-init would bind settings.invalidHandler to 'invalid-form.validate'
    // existingValdiation.init();
});
嘦怹 2024-12-15 06:51:47

您可以使用invalidHandler,我相信这是在jquery验证中。

invalidHandler 回调
提交无效表单时回调自定义代码。被称为
以事件对象作为第一个参数,验证器作为
第二。

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.

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