我真的不了解这种粗俗的自定义验证。 这不是我第一次遇到这个问题,而且我每次都放弃并以不同的方式将其破解。 但我想从现在开始。
我希望有一个条件验证器来检查(在这种情况下)minValue(0)或minValue(0.01),具体取决于页面上的其他内容。
如果未完全使用内置的“构造函数类型”验证器,则它们似乎不起作用:minValue: minValue(x)
经过几天的尝试,我什至无法解决这个问题。
// this works no problem, but is not conditional
dividend_interest: {
minValue: minValue(0)
}
// tried this, doesn't work
dividend_interest: {
minValue: this.bond.term_length === 0 ? minValue(0) : minValue(0.01),
}
// same with this
dividend_interest: {
minValue: function(val) {
return this.bond.term_length === 0 ? minValue(0) : minValue(0.01)
},
}
// also no success with this one
dividend_interest: {
minValue: minValue(this.bond.term_length === 0 ? 0 : 0.01),
}
怎么这么难?
官方文档https://vuelidate.js.org/#sub-optional-validator建议使用外部常量,返回true或false。 尽管这样做确实可行,但它对我没有帮助,因为它仅返回true或false,而不返回params(从etc生成错误消息),并且我无法访问vue实例的运行时属性(像这样)。在这种情况下为bond.term_length)。