简体   繁体   English

敲除验证多个级别的深层可观察数组

[英]knockout validating multiple levels deep observable array

Hi I need to create a custom validator that will be aplyed for each element of an observable array using knockout validation plugin. 嗨,我需要创建一个自定义验证器,使用敲除验证插件将其用于可观察数组的每个元素。 The structure of my object will look something like this when I post it to the server: 当我将对象发布到服务器时,其结构将如下所示:

var viewModel = {
   evaluationFormDataContract: {
       studentAssignmentInstanceId: value,
       evaluationType: value,
       categories: array[
       CategoriesOnEvaluationDataContract1 = {
           memo: value,
           categoryId: value,
           title: value,
           // Fields needed for validation
           hasMemo: value,
           memoIsMandatory: value
           questions: array[
           QuestionsOnEvalCategoryDataContract1 = {
               memo: value,
               grade: value,
               hasGrade: value,
               hasMemo: value,
               showOnlyMemo: value
           },
           QuestionsOnEvalCategoryDataContract2 = {
               memo: value,
               grade: value,
               hasGrade: value,
               hasMemo: value,
               showOnlyMemo: value
           }]
       },
       CategoriesOnEvaluationDataContract2 = {
           memo: value,
           categoryId: value,
           title: value,
           // Fields needed for validation
           hasMemo: value,
           memoIsMandatory: value
           questions: array[
           QuestionsOnEvalCategoryDataContract1 = {
               memo: value,
               grade: value,
               hasGrade: value,
               hasMemo: value,
               showOnlyMemo: value
           },
           QuestionsOnEvalCategoryDataContract2 = {
               memo: value,
               grade: value,
               hasGrade: value,
               hasMemo: value,
               showOnlyMemo: value
           },
           QuestionsOnEvalCategoryDataContract3 = {
               memo: value,
               grade: value,
               hasGrade: value,
               hasMemo: value,
               showOnlyMemo: value
           }]
       }, ]
   }

} }

Now the validation will have to be applyed only on the two nested arrays and will be done based on some properties. 现在,仅将验证应用于两个嵌套数组,并将基于某些属性完成验证。

The first validation has to be done on each object of the categories array and it will check if hasMemo and memoIsMandatory if this is the case memo will be required. 必须对category数组的每个对象进行首次验证,它将验证是否需要hasMemo和memoIsMandatory,如果是这种情况,则需要备忘。

The second validation will be done on each object of questions array and it will check if hasGrade if that is the case grade will be required. 第二个验证将在问题数组的每个对象上完成,它将检查hasGrade是否需要案例等级。

The last validation will be done on hasMemo and showOnlyMemo and are will be used for the memo value on the questions object. 最后一个验证将在hasMemo和showOnlyMemo上完成,并将用于问题对象上的备注值。

Reading the documentation for the validation plugin I found how I would extend a simple observable .Witch it seems to be done something like this: 阅读验证插件的文档后,我发现我将如何扩展一个简单的可观察的.Witch,它似乎是这样完成的:

ko.validation.rules['mustEqual'] = {
    validator: function (val, otherVal) {
        return val === otherVal;
    },
    message: 'The field must equal {0}'
};

But I do not think this will work for the structure of my viwmodel.How can I create validators for each observable in my observableArrays? 但是我认为这不适用于我的viwmodel的结构。如何为我的observableArrays中的每个可观察对象创建验证器?

First off, I agree with Tomalak. 首先,我同意Tomalak。 Rather than posting a bunch of nonsense that your code "looks something like", you should post some actual code that is readable. 您应该发布一些可读的实际代码,而不是发布一堆废话,使您的代码“看起来像”。 For instance, I can't tell if you are using any observable , computed or observableArray members, so I just have to assume that everything is observable or observableArray and there are no computed members. 例如,我无法确定您是否正在使用任何observablecomputedobservableArray成员,因此我只需要假设所有内容都是observableobservableArray ,并且没有computed成员即可。

Now, you said: 现在,您说:

The first validation has to be done on each object of the categories array and it will check if hasMemo and memoIsMandatory if this is the case memo will be required. 必须对category数组的每个对象进行首次验证,它将验证是否需要hasMemo和memoIsMandatory,如果是这种情况,则需要备忘。

Let me just say that naming a property hasMemo and that mean that the memo field is required is TERRIBLE! 我只说命名属性hasMemo ,这意味着memo字段是hasMemo If you call something hasMemo , it should mean that the thing in question has a memo . 如果您将某事物hasMemo ,则应表示该事物具有备忘录 And why would you need to look at both hasMemo and memoIsMandatory to see if memo is required? 以及为什么需要同时查看hasMemomemoIsMandatory来查看是否需要memo Ditto for hasGrade . hasGrade

Regardless, what you need is just to add validation to each of the observables on your classes. 无论如何,您只需要向类中的每个observables添加验证即可。 Wait, that's another assumption. 等等,这是另一个假设。 You are using classes, right? 您正在使用类,对吗? You're not just creating a single object and giving it a bunch of nested arrays and objects without using constructors, are you? 您不仅是在不使用构造函数的情况下创建单个对象并为其提供一堆嵌套的数组和对象,是吗? Well I'll proceed assuming that you're creating constructors and leave it at that. 好吧,我将假设您正在创建构造函数,然后继续进行。

I'll just focus on your first validation because the second one is just like it and the third one is unintelligible to me. 我将只关注您的第一个验证,因为第二个验证也是如此,而第三个验证对我来说是难以理解的。 So let's say your "CategoriesOnEvaluationDataContract1" object uses the following constructor: 因此,假设您的“ CategoriesOnEvaluationDataContract1”对象使用以下构造函数:

function Category() {
    var self = this;
    self.categoryId = ko.observable();
    self.hasMemo = ko.observable();
    self.memoIsMandatory = ko.observable();
    self.memo = ko.observable();
    //etc etc...
}

You need to extend memo with a validator, in this case you want the required validator. 您需要使用验证器来扩展memo ,在这种情况下,您需要所需的验证器。 That would look like this: 看起来像这样:

    self.memo = ko.observable().extend({ required: true });

This makes it so that memo is always required. 这样就使得始终需要memo But that is not what you want, you want it to be required when hasMemo and memoIsMandatory are both true, right?. 但这不是您想要的,当hasMemomemoIsMandatory都为true时,您希望它是必需的,对吗? So this is what you need to do: 因此,这是您需要做的:

    self.memo = ko.observable().extend({ required: { onlyIf: function() {
        return self.hasMemo() && self.memoIsMandatory();
    } } });

There. 那里。 That's all there is to it. 这里的所有都是它的。 You should be able to figure out the rest. 您应该能够弄清楚其余部分。 And if not, just let me know. 如果没有,请告诉我。 :) :)

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

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