3
votes

I need to validate Select option and some other fields using Formik and Yup .Actually other fields are validating but react-select couldn't .I tried some Codes from online not helping. My code like this

My Yup Rules

export const validationRules = Yup.object().shape({
  name: Yup.string().required('Customer name required'),
  email: Yup.string()
    .email('Invalid email')
    .required('Customer email required'),
  geography: Yup.object().required('Please select Geography'),
});

My Select option are

const geography = [
  { code: 'US', label: 'US', id: 1 },
  { code: 'SG', label: 'SG', id: 2 },
  { code: 'AUS', label: 'AUS', id: 3 },
  { code: 'OTHER', label: 'OTHER', id: 4 },
];

initial Data are

 const [intialData, setintialData] = useState({
    name: '',
    email: '',
    geography: {},
  });

I'am rendering like this

return(
      <Formik
        initialValues={intialData}
        validationSchema={validation rules}
        onSubmit={onSaveData}
      >
        {({ errors, setFieldValue, setFieldTouched, values, handleChange }) => (
          <Form noValidate>
            <div className="row">
              <div className="col">
                <label className="col-form-label-sm">Customer Name</label>
                <Field type="text" className="form-control" name="name" />
                {errors.name ? errors.name : ''}
              </div>
              <div className="col">
                <label className="col-form-label-sm">Customer Email</label>
                <Field type="email" className="form-control" name="email" />
                {errors.email ? errors.email : ''}
              </div>
            </div>
            <div className="row">
              <div className="col">
                <label className="col-form-label-sm">Geography</label>
                <Select
                  name="geography"
                  onBlur={() => setFieldTouched('geography', true)}
                  onChange={option => setFieldValue('geography', option)}
                  options={geography}
                />
                 {errors.geography ? errors.geography : ''}
              </div>
            </div>
            <div className="modal-footer">
              <button type="submit" className="btn btn-primary">
                Save changes
              </button>
            </div>
          </Form>
        )}
      </Formik>
)

Email and name fields are validated no react-select .So I consoled my Yup error object its displaying only name and email validation only not geography. Geography is initially empty object set in initial data please help me to validate react-select

2

2 Answers

2
votes

You should validate object like this:

name: Yup.string().required('Customer name required'),
email: Yup.string()
        .email('Invalid email')
        .required('Customer email required'),
geography: Yup.object()
            .shape({
              id: Yup.string()
            .required('Please, select office'),
          })

And you can get error from:

{errors.geography ? errors.geography.id : ''}
0
votes

Hello This is how Fixed the Issue and its worked..

               <Select
                  onBlur={() => setFieldTouched('gerography', true)}
                  onChange={(option, e) => {
                    handleChange('gerography', option);
                    this.onGeographyChange(option); //set values to state
                    setFieldValue('gerography', option);
                  }}
                  options={gerographys}
                  name="gerography"
                  value={state.gerography}
                />