自定义淘汰赛验证3参数 [英] Custom Knockout Validation 3 Parameters

查看:63
本文介绍了自定义淘汰赛验证3参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义的剔除验证规则,该规则将接受当前可观察值的值,与其进行比较的值以及一个条件.

I want to create a custom knockout validation rule that would take in the current observable's value, the value to compare it to, and a conditional.

我在网上看到的所有示例都只有两个参数传递到自定义验证中.

All of the examples I've seen on the web have only two parameters passed into the custom validation.

我已经做出了一种解决方法,将具有要比较的值和条件的值传递给数组,但这有点笨拙.我的消息格式也将输出数组中的值,而不仅仅是otherVal.

I have made a workaround to pass an array with the value to compare and the conditional, but this is a bit clunky. My message format outputs the values in the array as well instead of just the otherVal.

例如:

/*
* Compares two values only if the specified condition is true.
* Parameters: 
*   array[0] - otherVal
*   array[1] - conditional
*
* Returns:
*   true if the condition is false.
*   true if the condition is true AND val is lessThanOrEqualTo otherVal
*   false otherwise
*
* Usage:
*   Used to validate observables conditionally.
*
* Example:
*   self.salary = ko.observable().extend({ 
                      mustLessThanOrEqualConditional: [100, isFieldEditable]})
*/
ko.validation.rules['mustLessThanOrEqualConditional'] = {
  validator: function(val, array) {
      if (array[1]) {
          return val <= array[0];
      }
      return true;
  },
  message: 'The field must be <= {0}'
};

是否可以将三个参数传递给验证函数,例如:

Is there a way to pass three parameters into the validation function such as:

ko.validation.rules['mustLessThanOrEqualConditional'] = {
    validator: function(val, otherVal, condition) {
        if(condition){
            return val <= otherVal;
        }
        return true;
    },
    message: 'The field must be <= {0}'
}

推荐答案

您可以使用对象代替Array:

ko.validation.rules['mustLessThanOrEqualConditional'] = {
  validator: function(val, params) {
      if (params.condition) {
          return val <= params.otherVal;
      }
      return true;
  },
  message: 'The field must be <= {0}'
};

model.obs.extend({ mustLessThanOrEqualConditional: {
                       params: { otherVal: 3, condition: 1 != 0 }
                   }
                });

尽管就您而言,我还是建议使用

Although in your case, I would suggest using the onlyIf functionnality instead:

ko.validation.rules['mustLessThanOrEqualConditional'] = {
  validator: function(val, params) {
      return val <= params.otherVal;
  },
  message: 'The field must be <= {0}'
};

model.obs.extend({ mustLessThanOrEqualConditional: {
                       onlyIf: function () { /* your test here */ },
                       params: { otherVal: 3 }
                   }
                });

因为我是一个非常友好的人,所以这是我使用的规则:

As I am a very nice person, here is the rule I use:

ko.validation.rules['compareTo'] = {
    message: "Compare to",
    validator: function (val, params) {
        if (val === null || params.value() === null)
            return params.allowNull;
        switch (params.way) {
            case "<": return val < params.value();
            case "<=": return val <= params.value();
            case ">": return val > params.value();
            case ">=": return val >= params.value();
            default: throw new Error("params is not well defined");
        }
    }
}

model.obs.extend({ compareTo: { 
                       message:"your message", 
                       params: { 
                                   way: ">=", 
                                   value: model.otherObs, 
                                   allowNull: true 
                       }, 
                       onlyIf: function () { /*your test*/ } 
                    }});

这篇关于自定义淘汰赛验证3参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆