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

javascript - Date validation in Formik: 'Date of purchase' must be earlier than 'Date of sale' -

programmeradmin4浏览0评论

I am creating a simple form with date validation in React, Formik and Yup.

As you can see below, there are only two fields: datePurchase and dateSale. I set min and max parameters for both and I set them as required:

<Formik
  initialValues={{
    datePurchase: '',
    dateSale: '',
  }}
  validationSchema={object()
    .shape({
      datePurchase: date()
        .min(new Date('01-01-2019'))
        .max(new Date())
        .required(),
      dateSale: date()
        .min(new Date('01-01-2019'))
        .max(new Date())
        .required(),
    })}
>
  {({
    values, handleChange, handleBlur,
  }) => (
      <Form>
        <input
          name="datePurchase"
          type="date"
          onChange={handleChange}
          onBlur={handleBlur}
          value={values.datePurchase}
        />
        <input
          name="dateSale"
          type="date"
          onChange={handleChange}
          onBlur={handleBlur}
          value={values.dateSale}
        />
      </Form>
    )}
</Formik>

Then, I realized that datePurchase must be earlier than dateSale.

How to implement such condition using validationSchema() with Yup? Is that even possible?

I tried to use when() method with is: and then: params from Yup documentation, but to no avail.

I am creating a simple form with date validation in React, Formik and Yup.

As you can see below, there are only two fields: datePurchase and dateSale. I set min and max parameters for both and I set them as required:

<Formik
  initialValues={{
    datePurchase: '',
    dateSale: '',
  }}
  validationSchema={object()
    .shape({
      datePurchase: date()
        .min(new Date('01-01-2019'))
        .max(new Date())
        .required(),
      dateSale: date()
        .min(new Date('01-01-2019'))
        .max(new Date())
        .required(),
    })}
>
  {({
    values, handleChange, handleBlur,
  }) => (
      <Form>
        <input
          name="datePurchase"
          type="date"
          onChange={handleChange}
          onBlur={handleBlur}
          value={values.datePurchase}
        />
        <input
          name="dateSale"
          type="date"
          onChange={handleChange}
          onBlur={handleBlur}
          value={values.dateSale}
        />
      </Form>
    )}
</Formik>

Then, I realized that datePurchase must be earlier than dateSale.

How to implement such condition using validationSchema() with Yup? Is that even possible?

I tried to use when() method with is: and then: params from Yup documentation, but to no avail.

Share Improve this question asked Aug 19, 2019 at 15:47 plkpiotrplkpiotr 3742 gold badges9 silver badges25 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

Yup.ref allows you to reference other siblings - https://github./jquense/yup#yuprefpath-string-options--contextprefix-string--ref

Try giving this a go:

validationSchema={object()
    .shape({
      datePurchase: date()
        .min(Yup.ref('dateSale')
        .max(new Date())
        .required(),
      dateSale: date()
        .min(new Date('01-01-2019'))
        .max(new Date())
        .required(),
    })}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论