如何在禁用字段上停止yup验证?

我有以下内容:

  1. 两个字段:部门诊所
  2. 两个单选按钮部门诊所

    如果已选中诊所单选按钮,则该部门将被禁用。 现在,我希望yup仅在启用后才能验证诊所字段。我尝试过这样的事情

    Clinic:Yup.string().when       (‘isDisabled’,{ is: false,then: 
    Yup.string.required(‘clinic required’)})
zhaohevip 回答:如何在禁用字段上停止yup验证?

设置树字段,例如部门,诊所和 种类

种类 字段是单选按钮的名称,然后为部门和诊所等每个单选按钮设置值

,您的验证模式如下:

     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’),})
,
const object = {
  name: 'Foo',hiddenOnMobileField: 'Boo'
}

// you can use any condition attribute (Ex: isMobile)
export const validationSchema = (isMobile) => yup.object().shape({
  name: yup.string().trim().required('name is required'),// this field disabled on a mobile
  hiddenOnMobileField: yup.string()
    // use empty array
    .when([],{
      // any condition to remove validation
      is: () => !isMobile,then: yup.string().trim().required('hiddenOnMobileField is required'),otherwise: yup.string().notRequired(),})
})


validationSchema(true).validate(object,{ abortEarly: false })
  .then(...)
  .catch(...)
本文链接:https://www.f2er.com/3026129.html

大家都在问