How to use ko.validation.group function

前端 未结 2 379
我寻月下人不归
我寻月下人不归 2020-12-13 00:39

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

function exampleViewModel() {
   this.P1 = ko.observable().extend({ required :         


        
相关标签:
2条回答
  • 2020-12-13 01:11

    This worked well for me. Rather than grouping on this, create a proxy object that holds the properties you want validated.

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

    If you do this, consider using validatedObservable instead of group. Not only do you get the errors, but you can collectively check if all the properties are valid using the isValid property.

    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();
    
    0 讨论(0)
  • 2020-12-13 01:13

    As described in the documentation the right way to validate only specific observables is:

    this.errors = ko.validation.group([this.P1, this.P2, this.P3]);
    
    0 讨论(0)
提交回复
热议问题