【问题标题】:How to validate array of object inside array using Yup如何使用 Yup 验证数组内的对象数组
【发布时间】:2022-10-14 07:25:19
【问题描述】:

就像在我的情况下,我想验证一个位于数组数组内的表单。下面是架构。

var model = {
template_name: "",
severityLevels: [
    severity_id: 0,
    level_name: "",
    alert_escalations: [
        {
            id: 0,
            template_id: 0,
            severity_id: 0,
            level_id: 0,
            duration_seconds: {
                days: 0,
                hrs: 0,
                mins: 0
            },
            notification_count: 0,
            reminder_interval_seconds: {
                days: 0,
                hrs: 0,
                mins: 0
            },
            is_email_enabled: true,
            is_sms_enabled: true
        }
    ]
]
}

我想在severityLevels 内的alert_escalations 数组内验证duration_seconds 对象内的天数。如何实现此架构验证?我试过的是:

  var schema = Yup.object().shape({
    template_name: Yup.string().required("Template name field is required."),
    severityLevels: Yup.array().of({
      alert_escalations: Yup.array().of({
        duration_seconds: Yup.object().shape({
          days: Yup.string().required("Day field is required"),
        }),
      }),
    }),
    
  });

【问题讨论】:

    标签: reactjs material-ui react-hook-form yup


    【解决方案1】:

    我在媒体上看到了这个,我希望它有所帮助,因为它对我有所帮助。 Yup + formik dynamic array of object form validation

    【讨论】:

    猜你喜欢
    • 2021-12-21
    • 2020-12-11
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    • 2020-05-14
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    相关资源
    最近更新 更多