Knockout Validation组错误

时间:2014-03-21 01:12:04

标签: validation knockout.js

我正在尝试将firstName,lastName和EmailAddess设置为要验证的可观察组。

我一直得到的错误是“未定义ChecklistForm”。

ko.validation.rules.pattern.message = 'Invalid.';

ko.validation.configure({
  registerExtenders: true,
  messagesOnModified: true,
  insertMessages: false,
  parseInputAttributes: true,
  messageTemplate: null,
  errorClass: true
});


function checklistFormModel() {

  ChecklistForm = {

    something: ko.observable(false),
    somethingElse: function() {},

    firstName: ko.observable().extend({ minLength: 2, maxLength: 10 }),
    lastName: ko.observable().extend({ minLength: 2, maxLength: 10 }),
    emailAddress: ko.observable().extend({ email: true }),

    group: ko.validatedObservable({
      firstName: ChecklistForm.firstName(),
      lastName: ChecklistForm.lastName(),
      emailAddress: ChecklistForm.emailAddress()
    }),

    submitForm: function() {
      console.log( ChecklistForm.group.errors() );
      console.log( ChecklistForm.group.isValid() );
    }
  }
  return ChecklistForm;
}


ko.applyBindings(checklistFormModel);

1 个答案:

答案 0 :(得分:3)

更新了视图模型: -

function checklistFormModel() {

 var ChecklistForm = {

    something: ko.observable(false),
    somethingElse: function () {},

    firstName: ko.observable().extend({
        minLength: 2,
        maxLength: 10
    }),
    lastName: ko.observable().extend({
        minLength: 2,
        maxLength: 10
    }),
    emailAddress: ko.observable().extend({
        email: true
    }),

    group: ko.validatedObservable({
        firstName: this.firstName,
        lastName: this.lastName,
        emailAddress: this.emailAddress
    }),

    submitForm: function () {
        console.log(this.group.errors());
        console.log(this.group.isValid());
    }
 }
  return ChecklistForm;
}
ko.applyBindings(checklistFormModel);

Fiddle Demo