vee-validate验证之context使用:
vee-validate验证,通过context获取表单上下文信息:
let schema = string();
schema = schema.test(
"is-price-set-with-condition",
errorMessage,
function (value, context) {
console.log("context is:", context); // 打印出来是一个对象,其中有个 parent 属性,包括了当前表单的所有数据。还有path属性,这个path定义了当前验证字段名称
const fieldValue = context.parent[fieldForCondition]; // 也可以通过function的第二个参数 context 来获取,很有用
// const fieldValue = this.parent[fieldForCondition]; //之前用过 this.parent获取表单的其它字段
console.log("fieldValue is:", fieldValue)
return !(fieldValue && !value);
},
);
