reactjs - 根据状态变量进行条件 Yup 验证
问题描述
仅当给定变量的状态为真时,是否可以使用 Yup 解析器 RHF 有条件地验证文本字段。
我研究了很多,但找不到一个例子。
谢谢
解决方案
用于Yup.when
归档条件库验证。检查更多细节是的
例子:
您可以使用是的条件
const validationSchema = Yup.object().shape({
isCompany: Yup.boolean(),
companyName: Yup.string().when('isCompany', {
is: true,
then: Yup.string().required('Field is required')
}),
companyAddress: Yup.string().when('isCompany', {
is: (isCompany) => true,//just an e.g. you can return a function
then: Yup.string().required('Field is required'),
otherwise: Yup.string()
}),
});
并确保相应地更新您的表格。我希望你明白这一点。
推荐阅读
- android - 使用数据绑定时如何修复 InflateException 错误膨胀类片段
- reactjs - 如何在 ubuntu 上为数字海洋设置防火墙?
- c - 无法使用带有 FFTW 的 FFT 检索原始图像
- c++11 - ZeroMQ 服务器客户端启动顺序
- python - 测试功能为 Click 命令时 pytest 失败
- c# - 无法使用配置文件设置解决依赖关系
- sql-server - 对 Unicode 分隔符的批量插入支持
- c - 在“C”中的句子中交换单词
- scala - 如何使用 scala 模拟 Spark DataFrameReader?
- django - Celery 任务在从大型数据库中提取数据时中途失败