敲除映射验证 [英] Knockout Mapping Validation

查看:90
本文介绍了敲除映射验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将验证附加到映射视图. 我正在使用Knockout映射和验证插件. 伪模型:

I'm trying to attach validation to a mapped view. I'm using Knockout Mapping and Validation plugins. Pseudo-models:

Person {
    int Id;
    string Name;
    Book[] Books;
}

Book {
    int Id;
    string Name;
}

JavaScript:

Javascript:

function viewModel() {
    var self = this;
    self.persons = ko.observableArray();

    // persons are retrieved via AJAX...
    ko.mapping.fromJS(persons, {}, self.persons);
}

jQuery(function( $ ) {
    ko.applyBindings(new viewModel());
});

如何扩展人员observableArray来设置验证规则和消息? 我需要验证人员和书籍子数组属性. 我发现只有使用显式模型设置的示例,没有自动映射,例如:

How can I extend persons observableArray to set validation rules and message? I need to validate both persons and books sub-array properties. I've found only examples that use explicit model setting, without automatic mapping, something like:

Name: ko.observable().extend({ required: true })

然后,我需要设置ko.validatedObservable,isValid和validation.init,但是我真的不知道如何处理/组织它.您能提供一些帮助吗?

Then I'll need to set ko.validatedObservable, isValid and validation.init, but I really don't know how to handle/organize this. Can you please provide some help?

推荐答案

我发现至少有两种方法可以为通过ko.mapping插件创建的模型对象或模型对象提供验证:

I have found at least two ways to supply validations to model or view model objects that are created via the ko.mapping plugin:

  1. 在创建某些属性时,使用映射选项附加验证规则
  2. HTML5属性.仅某些验证(即必需的,模式)支持此功能.有关详细信息,请参见有关Knockout-Validation插件的文档

以上两种技术也可以结合使用.有关示例,请参见以下小提琴.

The above two techniques can also be combined. See the following fiddle for an example.

Knockout映射插件允许在要自定义的映射对象上创建某些属性.利用此功能,您可以覆盖插件的默认行为,并为映射的属性添加验证.下面是一个示例

The Knockout Mapping plugin allows the creation of certain properties on mapped objects to be customized. Taking advantage of this functionality, you can override the default behavior of the plugin and add validation for your mapped properties. Below is an example

HTML

<input data-bind="value: name" />


JavaScript

var data = { name: "Joe Shmo" };

var validationMapping = {
    // customize the creation of the name property so that it provides validation
    name: {
        create: function(options) {
            return ko.observable(options.data).extend( {required: true} );
        }
    }
};

var viewModel = ko.validatedObservable(ko.mapping.fromJS(data, validationMapping));
ko.applyBindings(viewModel);


Knockout Validation插件支持可在HTML控件中使用的一组有限的HTML5验证属性.但是,使用它们需要启用parseInputAttributes选项.这是一个简单的示例:

The Knockout Validation plugin supports a limited set of HTML5 validation attributes that can be used in your HTML controls. However, using them requires enabling the parseInputAttributes option. Here is a simple example:

HTML

<input data-bind="value: name" required />


JavaScript

// this can also be configured through the "validationOptions" binding (https://github.com/ericmbarnard/Knockout-Validation/wiki/Validation-Bindings)
ko.validation.configure({
    parseInputAttributes: true
});

var data = { name: "Joe Shmo" };

var viewModel = ko.validatedObservable(ko.mapping.fromJS(data));
ko.applyBindings(viewModel);

这篇关于敲除映射验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆