最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error: Cyclic dependency, node was:"dateA" - Stack Overflow

programmeradmin1浏览0评论

I want some information about the best implementation for a scenario like this: I have a select and two date pickers. When I choose from the select (idRisk) the parameter PRESENT, the two dates must be required. If DateA is before the DateB then in the form I want to show a validation error. What is the best way to achieve this? I write this for pletation, but show this error = Error: Cyclic dependency, node was:"dateA"

validationSchema: Yup.object({
    idRisk: Yup.number().required(),

    dateB: Yup.mixed().when('idRisk', {
        is: Risk.PRESENT,
        then: Yup.mixed().required(),
        otherwise: Yup.mixed()
    }),

    dateA: Yup.mixed()
        .when('idRisk', {
            is: Risk.PRESENT,
            then: Yup.mixed().required(),
            otherwise: Yup.mixed()
        })
        .when(['dateA', 'dateB'], (dateA, dateB) => {
            if (dateA.isBefore(dateB)) return this.required()
        })
})

I want some information about the best implementation for a scenario like this: I have a select and two date pickers. When I choose from the select (idRisk) the parameter PRESENT, the two dates must be required. If DateA is before the DateB then in the form I want to show a validation error. What is the best way to achieve this? I write this for pletation, but show this error = Error: Cyclic dependency, node was:"dateA"

validationSchema: Yup.object({
    idRisk: Yup.number().required(),

    dateB: Yup.mixed().when('idRisk', {
        is: Risk.PRESENT,
        then: Yup.mixed().required(),
        otherwise: Yup.mixed()
    }),

    dateA: Yup.mixed()
        .when('idRisk', {
            is: Risk.PRESENT,
            then: Yup.mixed().required(),
            otherwise: Yup.mixed()
        })
        .when(['dateA', 'dateB'], (dateA, dateB) => {
            if (dateA.isBefore(dateB)) return this.required()
        })
})
Share Improve this question edited Oct 9, 2021 at 12:03 Akash Kumar Verma 3,3382 gold badges18 silver badges33 bronze badges asked Feb 21, 2019 at 10:23 JessicaJessica 511 gold badge1 silver badge3 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

you cannot referrer the parameter ["dateA"] inside dateA:Yup.mixed().when() method, consider to use different approach, on example:

dateA: Yup.mixed()
  .when(["dateB"],
    (dateB, schema, node) => {
      if (node.value.isBefore(dateB))
        return this.required();
    }
  )

you can get dateA value from node.value

You have to add cyclic dependency at the end like this --

validationSchema: Yup.object({
idRisk: Yup.number().required(),

dateB: Yup.mixed().when('idRisk', {
    is: Risk.PRESENT,
    then: Yup.mixed().required(),
    otherwise: Yup.mixed()
}),

dateA: Yup.mixed()
    .when('idRisk', {
        is: Risk.PRESENT,
        then: Yup.mixed().required(),
        otherwise: Yup.mixed()
    })
    .when(['dateA', 'dateB'], (dateA, dateB) => {
        if (dateA.isBefore(dateB)) return this.required()
    })}, ['dateA'])
发布评论

评论列表(0)

  1. 暂无评论