简体   繁体   English

如何使用敲除验证

[英]how to use knockout validation

I am following this link to create validations. 我正在跟踪此链接以创建验证。

But i dont understand how can i use this extend method in my code. 但是我不明白如何在我的代码中使用此扩展方法。

I load data into my observable with records coming from calling breeze query. 我使用调用微风查询的记录将数据加载到我的可观察对象中。

I load data in below manner 我以以下方式加载数据

dataObsArray= ko.observableArray()

datacontext.getData(id,dataObsArray)
                   .then(function () {

          // some logic


 })
        .fail("Data not found");

Then i bind this obs array to my view as below 然后我将这个obs数组绑定到我的视图,如下所示

<tbody data-bind="with: dataObsArraay">
            <tr>
                <td>name</td>
                <td> <input data-bind="  value: Name" ></td>
                <td> <input data-bind="  value: Age" ></td>

            </tr>
</tbody>

So i dont understand how can i use extend method because i am just using binding my view with properties in my observable array. 所以我不明白如何使用扩展方法,因为我只是在用可观察数组中的属性绑定视图。

Please guide me. 请指导我。

Consider using breeze validation instead of putting the validation logic in the UI code via a knockout extender. 考虑使用微风验证,而不是通过敲除扩展程序将验证逻辑放入UI代码中。 Using breeze validation will ensure the rules are always evaluated and will save you from creating an extra model over your entity for the purposes of validation. 使用微风验证可以确保始终对规则进行评估,并且可以避免为了验证而在实体上创建额外的模型。

here's an example using one of breeze's built in validators: the stringLength validator. 这是一个使用breeze内置验证器之一的示例:stringLength验证器。

var entityType = entityManager.metadataStore.getEntityType('????'),
    nameProperty = entityType.getProperty('Name'),
    nameLengthValidator = breeze.Validator.stringLength({ maxLength: 10, minLength: 2 });
nameProperty.validators.push(nameLengthValidator);

here's an example of a custom required validator for strings that doesn't allow whitespace-only values: 这是一个不允许使用纯空白值的字符串的自定义必需验证器的示例:

// make a reusable validator
var myRequiredValidator = breeze.Validator.makeRegExpValidator(
    "myRequiredValidator",  
    /\S/,  
    "The %displayName% '%value%' cannot be blank or entirely whitespace");

// register it with the breeze Validator class.
breeze.Validator.register(myRequiredValidator);

// add the validator to the Name property...
var entityType = entityManager.metadataStore.getEntityType('????'),
    nameProperty = entityType.getProperty('Name');
nameProperty.validators.push(nameLengthValidator);

here's the documentation for making regex validators. 这是制作正则表达式验证器的文档

You can also write custom validators- check the breeze docs for more info on that- look for the Write a custom validator section. 您还可以编写自定义验证器-查看微风文档以获取更多信息-寻找“ 编写自定义验证器”部分。

you'll need to create a model for your data, eg: 您需要为数据创建一个模型,例如:

function person(name, age) {
    this.name = ko.observable(name).extend({ minLength: 2, maxLength: 10 });
    this.age = ko.observable(age).extend({ min: 18, max: 99 });
}

var data = [],
    people = ko.observableArray();

datacontext.getData(id, data)
    .then(function (data) {
        for (i = 0; i < data.length; i++) {
            people.push(new person(data.Name, data.Age));
        }
    })
    .fail("Data not found");


<tbody data-bind="foreach: people">
    <tr>
        <td>name</td>
        <td> <input data-bind="  value: name" ></td>
        <td> <input data-bind="  value: age" ></td>
    </tr>
</tbody>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM