Knockout Validation onlyif object no function and pattern validation

瘦欲@ 提交于 2019-12-04 05:41:48

问题


I want to the priceMax was required, when title is empty.

I have code

self.searchParameters = {
    title: ko.observable().extend({
      refreshCountOffers: 500
    }),
    priceMax: ko.observable().extend({
      required: {
        onlyIf: function() {
          return  title==null;
        }
      },
      refreshCountOffers: 500
    })
  };

,but I get error 'title is not defined'.

How to disable option, which show error for pattern validation, when user input first letter?

postCode: ko.observable().extend({
  required: true,
  pattern: {
    message: 'Post code is not valid',
    params: '[0-9]{2}-[0-9]{5}'
  },
  refreshCountOffers: 500
})

my jsfiddle


回答1:


There are 2 problems with the onlyIf check:

  1. you need to qualify the title property
  2. title is observable so you need to access the value

The code below resolves both:

onlyIf: function() {
    return self.searchParameters.title();
}


来源:https://stackoverflow.com/questions/38498607/knockout-validation-onlyif-object-no-function-and-pattern-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!