use*_*719 2 javascript validation yup formik
我有以下几点:
两个领域:科室和诊所
两个单选按钮部门和诊所
如果选中诊所单选按钮,则该部门被禁用。现在我希望是的,仅在启用诊所字段时才对其进行验证。我试过这样的事情
// Clinic:
Yup.string().when('isDisabled', {
is: false,
then: Yup.string.required('clinic required')
})
Run Code Online (Sandbox Code Playgroud)
小智 10
设置树字段,例如部门、诊所和种类
该种场单选按钮的名称,然后设置值,像部门和诊所的每个单选按钮
你的验证架构是这样的:
validationSchema=Yup.object().shape({
kind: Yup.string().required(),
clinic: Yup.string().when("kind", {
is: (val) => val === "clinic",
then: Yup.string().required(‘clinic required’),
otherwise: Yup.string().notRequired()
}),
department: Yup.string().when("kind", {
is: (val) => val === "department",
then: Yup.string().required(‘department required’),
otherwise: Yup.string().notRequired()
}),
})
Run Code Online (Sandbox Code Playgroud)