Jquery 如何实现backbone.js验证/自定义验证?
需要使用主干验证js根据其他字段的值验证一个字段。如何处理这个问题?是否可以使用诸如range validator、max validator之类的lib方法进行验证,还是应该使用自定义方法 通常情况下是这样的Jquery 如何实现backbone.js验证/自定义验证?,jquery,backbone.js,requirejs,marionette,backbone.validation.js,Jquery,Backbone.js,Requirejs,Marionette,Backbone.validation.js,需要使用主干验证js根据其他字段的值验证一个字段。如何处理这个问题?是否可以使用诸如range validator、max validator之类的lib方法进行验证,还是应该使用自定义方法 通常情况下是这样的 bindings: { 'value1': { Observe: 'value1', Setoptions:{ Validate:true } } } 这将调用validate方法 Va
bindings: {
'value1': {
Observe: 'value1',
Setoptions:{
Validate:true
}
}
}
这将调用validate方法
Validation: {
Value1: {
Pattern: number,
Min: 0
/* here we need to validate value1 < value2 where value 2 is value of another input field */
}
}
验证:{
价值1:{
图案:数字,
最低:0
/*这里我们需要验证value1
谢谢我看不到任何东西适合比较两个值,请参见 如果重复使用,可以使用
自定义验证器。这里使用验证程序方法
Backbone.Model.extend({
validation: {
value1: function(value, attr, computedState) {
if(value < computedState.value2) {
return 'Error message';
}
}
}
});
你可以用
并使用以下两种情况中的一种:
或
我使用了命名的方法验证程序,非常感谢。。。资料来源:
Backbone.Model.extend({
validation: {
value1: {
min: 0,
pattern: 'number',
fn: 'greaterThan'
}
},
greaterThan: function(value, attr, computedState) {
if(value < computedState.value2) {
return 'Error message';
}
}
});
validation: {
attribute: {
required: function(val, attr, computed) {
return computed.someOtherAttribute === 'foo';
},
length: 10
}
}
_.extend(Backbone.Validation.validators, {
custom: function(value, attr, customValue, model) {
return this.length(value, attr, 4, model) || this.custom2(value, attr, customValue, model);
},
custom2: function(value, attr, customValue, model) {
if (value !== customValue) {
return 'error';
}
}
});