Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何实现backbone.js验证/自定义验证?_Jquery_Backbone.js_Requirejs_Marionette_Backbone.validation.js - Fatal编程技术网

Jquery 如何实现backbone.js验证/自定义验证?

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

需要使用主干验证js根据其他字段的值验证一个字段。如何处理这个问题?是否可以使用诸如range validator、max validator之类的lib方法进行验证,还是应该使用自定义方法

通常情况下是这样的

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';
    }
  }
});