32

I am trying to use knockout.validation plugin. I created an exampleViewModel :

function exampleViewModel() {
   this.P1 = ko.observable().extend({ required : true });
   this.P2 = ko.observable().extend({ required : true });
   this.P3 = ko.observable().extend({ required : true });
   this.P4 = ko.observable().extend({ required : true });

   this.errors = ko.validation.group(this);
}    

In the above view model i created a validation group named errors for the current object. Now if any validation rule fails on any 1 property out of 4 than this errors property contains an error message.

My question is , if i want to create a validation group of only 3 properties (P1, P2, P3) out of 4 than how can i do this ?

4

2 に答える 2

58

これは私にとってうまくいきました。でグループ化するのではなく、this検証するプロパティを保持するプロキシオブジェクトを作成します。

this.errors = ko.validation.group({
    P1: this.P1,
    P2: this.P2,
    P3: this.P3
});

これを行う場合は、validatedObservableの代わりに使用することを検討してくださいgroup。エラーが発生するだけでなく、プロパティを使用してすべてのプロパティが有効かどうかをまとめて確認できisValidます。

this.validationModel = ko.validatedObservable({
    P1: this.P1,
    P2: this.P2,
    P3: this.P3
});

// is the validationModel valid?
this.validationModel.isValid();
// what are the error messages?
this.validationModel.errors();
于 2012-10-24T02:43:53.373 に答える