Conditional Validation in Yup

后端 未结 3 724
刺人心
刺人心 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 01:53

    Totally agree with @João Cunha's answer. Just a supplement for the use case of Radio button.

    When we use radio button as condition, we can check value of string instead of boolean. e.g. is: 'Phone'

    const ValidationSchema = Yup.object().shape({
      // This is the radio button.
      preferredContact: Yup.string()
        .required('Preferred contact is required.'),
      // This is the input field.
      contactPhone: Yup.string()
        .when('preferredContact', {
          is: 'Phone',
          then: Yup.string()
            .required('Phone number is required.'),
        }),
      // This is another input field.
      contactEmail: Yup.string()
        .when('preferredContact', {
          is: 'Email',
          then: Yup.string()
            .email('Please use a valid email address.')
            .required('Email address is required.'),
        }),
    
    });
    

    This the radio button written in ReactJS, onChange method is the key to trigger the condition checking.

    <label>
      <input
        name="preferredContact" type="radio" value="Email"
        checked={this.state.preferredContact == 'Email'}
        onChange={() => this.handleRadioButtonChange('Email', setFieldValue)}
      />
      Email
    </label>
    <label>
      <input
        name="preferredContact" type="radio" value="Phone"
        checked={this.state.preferredContact == 'Phone'}
        onChange={() => this.handleRadioButtonChange('Phone', setFieldValue)}
      />
      Phone
    </label>
    

    And here's the callback function when radio button get changed. if we are using Formik, setFieldValue is the way to go.

    handleRadioButtonChange(value, setFieldValue) {
      this.setState({'preferredContact': value});
      setFieldValue('preferredContact', value);
    }
    
    0 讨论(0)
  • 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:

    <Formik 
      initialValues={{ email: '', showEmail: false }}
      validationSchema={Yup.object().shape({
        showEmail: Yup.boolean(),
        email: Yup
          .string()
          .email()
          .when("showEmail", {
            is: true,
            then: Yup.string().required("Must enter email address")
          })
      })
    }
    
    />
    
    0 讨论(0)
  • 2020-12-29 02:19

    You probably aren't defining a validation rule for the showEmail field.

    I've done a CodeSandox to test it out and as soon as I added:

    showEmail: yup.boolean()
    

    The form started validation correctly and no error was thrown.

    This is the url: https://codesandbox.io/s/74z4px0k8q

    And for future this was the correct validation schema:

    validationSchema={yup.object().shape({
        showEmail: yup.boolean(),
        email: yup
          .string()
          .email()
          .when("showEmail", {
            is: true,
            then: yup.string().required("Must enter email address")
          })
      })
    }
    
    0 讨论(0)
提交回复
热议问题