Rom*_*man 5 javascript forms validation reactjs yup
您好,我正在尝试找到一种方法来比较两个字段并仅在它们不相等时进行验证。
这是我能想到的唯一想法,但它不起作用:
yup
.number()
.required()
.notOneOf(
[FormField.houseHoldMembers as any],
'error message',
),
Run Code Online (Sandbox Code Playgroud)
您可以比较两个值并仅在它们不相等时进行验证,如下所示:
const mySchema = yup.object({
text1: yup.number().required(),
text2: yup
.number()
.required()
.when(["text1"], (text1, schema) => {
console.log(schema);
return schema.notOneOf([text1], "the two values should not be equal");
})
});
Run Code Online (Sandbox Code Playgroud)
您可以查看此沙箱,了解该解决方案的实时工作示例。