在进行条件验证时,如何在 Breeze 中将实体引用传递给验证上下文?
考虑以下场景:
我有单选按钮列表,它不直接绑定(bind)到 Breeze 实体,但绑定(bind)到我的 View 模型属性。根据单选按钮的选择,我想验证表单上绑定(bind)到我的 Breeze 数据实体的其他输入。
我尝试在单选按钮上构建自定义验证,其中我的验证函数在验证上下文的内置值属性中保存对单选按钮选择的引用。现在,我尝试在验证函数中访问我的实体对象,以根据单选按钮选择评估表单的其他输入。
最佳答案
这是最大长度验证的任意示例。使用上下文访问实体并添加您需要的逻辑。
function maxLengthValidatorFn(value, context) {
var someValue = context.entity.SomeProperty;
//Do something with some value.
if (value && value.length > context.maxLength)
return false;
return true;
}
function maxLengthValidatorFactory(context) {
return new breeze.Validator(
"maxLength",
maxLengthValidatorFn,
{ messageTemplate: "'%hrn%' exceeds maximum character length of %maxLength%", hrn: context.propertyLabel, maxLength: context.maxLength }
);
}
关于validation - Breeze 自定义验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20961784/