javascript - 是的,不验证何时满足条件
问题描述
我有其他字段,只有在选中复选框时才会显示(布尔值为 true)。这些附加字段也需要验证,但使用 .when() 方法似乎不会对复选框为真作为条件做出反应。我的代码的验证部分如下所示:
const schema = useMemo(
() =>
yup
.object()
.shape({
type: yup
.string()
.label(translate('Forms.EmailForm.type.label'))
.required(),
person: yup
.object()
.shape({
firstName: yup
.string()
.required()
.trim()
.min(1)
.max(255)
.label(
translate(
'Forms.EmailForm.firstName.label',
),
),
hasAddress: yup.boolean(),
address: yup.boolean().when('hasAddress', {
is: true,
then: yup
.object()
.shape({
firstLine: yup
.string()
.required()
.max(255)
.label(
translate(
'Forms.EmailForm.address.firstLine.label',
),
),
secondLine: yup
.string()
.max(255)
.label(
translate(
'Forms.EmailForm.address.secondLine.label',
),
),
}),
}),
}),
}),
[],
);
const form = useForm<EmailForm>({
resolver: yupResolver(schema),
defaultValues: {
type: 'PERSON',
},
});
解决方案
推荐阅读
- python - subprocess.check_output 显示忽略异常时的错误输出
- reactjs - Why calling useState function is working without declaring it?
- javascript - 允许用于 chrome 扩展清单匹配的 URI 方案
- java - 为什么我在这里得到 java.lang.ArrayIndexOutOfBoundsException?
- bash - SSH 从 Ubuntu 到 Windows 7
- discord - 有人可以解释对 bulkDelete 的更改(discord.js)
- node.js - 运行 npm dev 时出现节点错误 (webpack-cli)。无法弄清楚错误消息的含义
- python - KeyError:python 中的“名称”,同时尝试将 csv 导入 dict
- python - QTableView() 仅在选择时更新更改
- php - 如何使用递归在 PHP 中操作嵌套数组?