Knockout Validation onlyif object no function and

2019-03-04 20:07发布

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条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-03-04 20:37

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();
}
查看更多
登录 后发表回答