Conditional Validation in Yup

后端 未结 3 723
刺人心
刺人心 2020-12-29 01:28

I have an email field that only gets shown if a checkbox is selected (boolean value is true). When the form get submitted, I only what this field to be required

3条回答
  •  执笔经年
    2020-12-29 02:06

    Formik author here...

    To make Yup.when work properly, you would have to add showEmail to initialValues and to your Yup schema shape.

    In general, when using validationSchema, it is best practices to ensure that all of you form's fields have initial values so that Yup can see them immediately.

    The result would look like:

    
    

提交回复
热议问题