0

I am doing a form validation in reactjs using YUP. I wrote the form validation as follows -

const FromValidation = Yup.object().shape({
    nameId: Yup.object()
    .nullable()
    .required('name is requried'),
    classId: Yup.object()
    .nullable()
    .required('class is required'),
    statusId: Yup.object()
    .nullable()
    .when('classId',{
        is:(val) => val === 1,
        then: Yup.object()
            .nullable()
            .required('status is required')
    }
})

The null validations are working fine. But validation for statusid is not working. I want to display 'status is required' only if classId is 1. Kindly point me out where I am going wrong. Thanks in advance.

coddey
  • 390
  • 2
  • 13
  • Your schema states that `classId` is an object. How can an object equal `1`? – Jake Worth Sep 14 '20 at 18:50
  • @JakeWorth Hello Jake thanks for the comment. Can you please suggest me how can I do the validation. I am novice in Reatjs. Thanks. – coddey Sep 15 '20 at 11:10
  • @JakeWorth class is a dropdownlist. The classId = 1 corresponds to 'TinyTots'. If I put val === 'TinyTots', its still not working. Could you please point out how can I resolve this issue. Thanks in advance. – coddey Sep 15 '20 at 11:29
  • What is the value of `classId` select when you want to require status? Focusing the select in Chrome DevTools and then executing `$0.value` in the console would tell you. That value is the condition you need to match in your `is`. – Jake Worth Sep 15 '20 at 14:16

0 Answers0