小编Afr*_*har的帖子

是的,条件验证

我想使用我的架构在其他字段的基础上验证一个字段。就像,如果 carType 是“SUV”,则最大“noOfPassengers”应该是 6,否则是 4。但是,它没有验证这种情况,我既无法调试它,也无法在控制台中收到任何错误。

const validationSchema = yup.object().shape({
    location: yup
        .string()
        .required('location is required'),
    destination: yup
        .string()
        .required('Destination is required'),
    carType: yup
        .string(),
    noOfPassengers: yup
        .string()
        .when('carType', {
            is: value => value && value === "SUV",
            then: yup
                .string()
                .max(6, 'Max 6 passengers are required'),
            otherwise: yup
                .string()
                .max(4, 'Max 4 passengers are required'),
        }),
});
Run Code Online (Sandbox Code Playgroud)

javascript typescript reactjs yup formik

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

formik ×1

javascript ×1

reactjs ×1

typescript ×1

yup ×1