Knockout仅验证验证对象无功能和模式验证 [英] Knockout Validation onlyif object no function and pattern validation
本文介绍了Knockout仅验证验证对象无功能和模式验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有代码
self.searchParameters = {
title:ko.observable()。extend({
refreshCountOffers:500
}),
priceMax:ko。 observable()。extends({
required:{
onlyIf:function(){
return title == null;
}
},
refreshCountOffers :500
})
};
,但是我收到错误'title is not defined'。
当用户输入第一个字母时,如何禁用选项,显示模式验证的错误?
postCode: ko.observable()。extend({
required:true,
pattern:{
message:'Post code is not valid',
params:'[0-9] {2} - [0-9] {5}'
},
refreshCountOffers:500
})
我的 jsfiddle
解决方案
只有有2个问题检查:
- 您需要符合
标题
属性 -
title
是可观察的,所以您需要访问值
下面的代码解决了:
onlyIf:function(){
return self.searchParameters.title();
}
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
解决方案
There are 2 problems with the onlyIf
check:
- you need to qualify the
title
property title
is observable so you need to access the value
The code below resolves both:
onlyIf: function() {
return self.searchParameters.title();
}
这篇关于Knockout仅验证验证对象无功能和模式验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文