敲除验证

发布于 2024-12-28 16:43:56 字数 100 浏览 0 评论 0原文

我有一个 asp.net mvc3 项目,我在其中使用淘汰赛绑定对表进行批量编辑。我想在保存数据时进行必需验证和数字验证等验证。有没有更简单的方法来进行淘汰验证。 PS:我没有使用表格。

I have asp.net mvc3 project where I do a bulk edit on a table with knockout binding. I want to do validations like required and number validations while saving data. Is there any easier way to do knock out validations.
PS: I am not using forms.

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

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

发布评论

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

评论(3

不一样的天空 2025-01-04 16:43:56

看一下 Knockout-Validation ,它干净地设置并使用 淘汰文档。下:实时示例 1:强制输入为数字

您可以在 Fiddle 中实时查看

更新: fiddle 已更新为使用 cloudfare CDN url 来使用最新的 KO 2.0.3 和 ko.validation 1.0.2

要设置 ko.validation:

ko.validation.rules.pattern.message = 'Invalid.';

ko.validation.configure({
    registerExtenders: true,
    messagesOnModified: true,
    insertMessages: true,
    parseInputAttributes: true,
    messageTemplate: null
});

要设置验证规则,请使用扩展器。例如:

var viewModel = {
    firstName: ko.observable().extend({ minLength: 2, maxLength: 10 }),
    lastName: ko.observable().extend({ required: true }),
    emailAddress: ko.observable().extend({  // custom message
        required: { message: 'Please supply your email address.' }
    })
};

Have a look at Knockout-Validation which cleanly setups and uses what's described in the knockout documentation. Under: Live Example 1: Forcing input to be numeric

You can see it live in Fiddle

UPDATE: the fiddle has been updated to use the latest KO 2.0.3 and ko.validation 1.0.2 using the cloudfare CDN urls

To setup ko.validation:

ko.validation.rules.pattern.message = 'Invalid.';

ko.validation.configure({
    registerExtenders: true,
    messagesOnModified: true,
    insertMessages: true,
    parseInputAttributes: true,
    messageTemplate: null
});

To setup validation rules, use extenders. For instance:

var viewModel = {
    firstName: ko.observable().extend({ minLength: 2, maxLength: 10 }),
    lastName: ko.observable().extend({ required: true }),
    emailAddress: ko.observable().extend({  // custom message
        required: { message: 'Please supply your email address.' }
    })
};
勿忘初心 2025-01-04 16:43:56

如果您不想使用 KnockoutValidation 库,您可以编写自己的库。以下是必填字段的示例。

添加一个带有所有 KO 扩展或扩展器的 javascript 类,并添加以下内容:

ko.extenders.required = function (target, overrideMessage) {
    //add some sub-observables to our observable
    target.hasError = ko.observable();
    target.validationMessage = ko.observable();

    //define a function to do validation
    function validate(newValue) {
    target.hasError(newValue ? false : true);
    target.validationMessage(newValue ? "" : overrideMessage || "This field is required");
    }

    //initial validation
    validate(target());

    //validate whenever the value changes
    target.subscribe(validate);

    //return the original observable
    return target;
};

然后在您的 viewModel 中通过以下方式扩展您的可观察对象:

self.dateOfPayment: ko.observable().extend({ required: "" }),

网上有很多这种验证方式的示例。

If you don't want to use the KnockoutValidation library you can write your own. Here's an example for a Mandatory field.

Add a javascript class with all you KO extensions or extenders, and add the following:

ko.extenders.required = function (target, overrideMessage) {
    //add some sub-observables to our observable
    target.hasError = ko.observable();
    target.validationMessage = ko.observable();

    //define a function to do validation
    function validate(newValue) {
    target.hasError(newValue ? false : true);
    target.validationMessage(newValue ? "" : overrideMessage || "This field is required");
    }

    //initial validation
    validate(target());

    //validate whenever the value changes
    target.subscribe(validate);

    //return the original observable
    return target;
};

Then in your viewModel extend you observable by:

self.dateOfPayment: ko.observable().extend({ required: "" }),

There are a number of examples online for this style of validation.

甜是你 2025-01-04 16:43:56

Knockout.js 验证很方便,但不够健壮。您始终必须创建服务器端验证副本。在您的情况下(当您使用 Knockout.js 时),您将 JSON 数据异步发送到服务器并返回,因此您可以让用户认为他看到了客户端验证,但实际上这将是异步服务器端验证。

看一下这里的示例 upida.cloudapp.net :8080/org.upida.example.knockout/order/create?clientId=1
这是“创建订单”链接。尝试点击“保存”,然后玩产品。
这个例子是使用codeplex的upida库(这个库有spring mvc版本和asp.net mvc)完成的。

Knockout.js validation is handy but it is not robust. You always have to create server side validation replica. In your case (as you use knockout.js) you are sending JSON data to server and back asynchronously, so you can make user think that he sees client side validation, but in fact it would be asynchronous server side validation.

Take a look at example here upida.cloudapp.net:8080/org.upida.example.knockout/order/create?clientId=1
This is a "Create Order" link. Try to click "save", and play with products.
This example is done using upida library (there are spring mvc version and asp.net mvc of this library) from codeplex.

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