简体   繁体   English

使用和 react-hook-form 进行 React yup 验证,不同类型对象的数组取决于 object 属性之一

[英]React yup validation with and react-hook-form, array of different type of objects depends from one of object attribute

I have a form, where user can add with no limit ( add shareholder ).我有一个表格,用户可以无限制地添加(添加股东)。

Shareholder can be 2 different types ( NATURAL / LEGAL )股东可以是 2 种不同的类型(自然/法律)

here is interface for thouse objects:这是 thouse 对象的接口:

export interface shareholderNatural {
  type: 'NATURAL';
  firstName: string;
  lastName: string;
  personalCode: string;
  sharePart: number;
  politicallyExposed: boolean;
  country: string;
  position: string;
}

export interface shareholderLegal {
  type: 'LEGAL';
  companyName: string;
  companyCode: string;
  sharePart: number;
  politicallyExposed: boolean;
  country: string;
  position: string;
}

I need validation for: firstName, lastName, personalCode, sharePart if type is NATURAL companyName, companyCode, sharePart if type is NATURAL我需要验证:firstName、lastName、personalCode、sharePart 如果类型是 NATURAL companyName、companyCode、sharePart 如果类型是 NATURAL

Attribut type is set by the select from the component state when user click opn add another sharehoilder button (useState)当用户单击 opn 添加另一个共享者按钮 (useState) 时,属性类型由组件 state 中的 select 设置

I tryed to do it with Yup.array().when( 'key', {})我试着用 Yup.array().when('key', {})

 const validationSchema = Yup.object().shape({
shareholders: Yup.array().when('type', {
  is: 'NATURAL',
  then: Yup.array().of(
    Yup.object().shape({
      firstName: Yup.string().required(t('')),
      lastName: Yup.string().required(t('')),
      personalCode: Yup.string().required(t('')),
      sharePart: Yup.number().required(t('')),
    }),
  ),
  otherwise: Yup.array().of(
    Yup.object().shape({
      companyName: Yup.string().required(t('')),
      companyCode: Yup.string().required(t('')),
      sharePart: Yup.number().required(t('')),
    }),
  ),
}),

}); });

But in this case key attribute should be outside...但在这种情况下,关键属性应该在外面......

Any help here?这里有什么帮助吗? Thank you.谢谢你。

You can check every field separately.您可以分别检查每个字段。 Just replace type with needed field:只需将type替换为所需字段:

 const validationSchema = Yup.object().shape({ shareholders: Yup.array().of( Yup.object().shape({ firstName: Yup.string().when('type', { is: 'NATURAL', then: Yup.string().required(t('')) }), lastName: Yup.string().when('type', { is: 'NATURAL', then: Yup.string().required(t('')) }), personalCode: Yup.string().when('type', { is: 'NATURAL', then: Yup.string().required(t('')) }), sharePart: Yup.number().required(t('')), companyName: Yup.string().when('type', { is: 'LEGAL', then: Yup.string().required(t('')) }), companyCode: Yup.string().when('type', { is: 'LEGAL', then: Yup.string().required(t('')) }), }) ) ) };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用带有react-hook-form的yup验证模式跳过对fieldArray表单中最后一个object的验证? - How to skip validation for last object in fieldArray form using yup validation schema with react-hook-form? 在 Yup Validation React-Hook-Form 中添加了 2 个 Schema - Added 2 Schema in Yup Validation React-Hook-Form 使用 react-hook-form 和 Yup 验证子输入类型文件 - Validating a child input type file with react-hook-form and Yup 使用 React-Hook-Form 和 Yup 验证文件(<x> 必须是 `object` 类型,但最终值为:`null`)</x> - Validating File using React-Hook-Form and Yup (<x> must be a `object` type, but the final value was: `null`) 如何使用 React-hook-form 验证 React-quill? - How to validate React-quill with React-hook-form and yup? 是的验证层次结构问题(formik vs react-hook-form with yupResolver) - yup validation hierarchy issue (formik vs react-hook-form with yupResolver) 如何在“react-hook-form”库中的控制器组件中使用 Yup 验证模式 - How to use Yup validation schema with the Controller component in `react-hook-form` lib react-hook-form yup schema useFieldArray 验证错误消息不是 go away - react-hook-form yup schema useFieldArray validation error message not go away 如何在用户单击提交按钮之前以 react-hook-form 触发 yup 验证? - How to trigger yup validation in react-hook-form before the user is clicking the submit button? react-hook-form 验证不适用于自定义字段数组 - react-hook-form validation not working with custom fields array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM