Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

289
Views
Need to set up yup validation which is not required and works on only one condition

I added yup validation in the date picker so what is happening now if I click on submit it says field required Invalid Date! but my task needs to set validation such like the error will generate only if the date is greater than today's date that I have added in max and also it is not added in any .required("Required!") thing but not sure why it's working as Required! field which I don't need to set as Required!

const today = moment().format("YYYY-MM-DD");
const validationSchema = yup.object().shape({
  date_of_birth: yup
    .date()
    .max(today, "DOB cannot be greater than today's date")
    .typeError("Invalid Date!")
});

export default function MaterialUIPickers() {
  const IndivisualForm = useFormik({
    initialValues: {
      date_of_birth: null
    },
    enableReinitialize: true,
    validationSchema,
    onSubmit: (values) => {
    }
  });
  const handleChangeDate = (date) => {
    IndivisualForm.setFieldValue(
      "date_of_birth",
      moment(date).format("YYYY-MM-DD")
    );
  };

  return (
    <MuiPickersUtilsProvider utils={DateFnsUtils}>
      <Grid container justifyContent="space-around">
        <KeyboardDatePicker
          disableToolbar
          variant="inline"
          format="MM/dd/yyyy"
          margin="normal"
          id="date-picker-inline"
          label="Date picker inline"
          value={IndivisualForm.values.date_of_birth}
          error={Boolean(IndivisualForm.errors.date_of_birth)}
          helperText={IndivisualForm.errors.date_of_birth}
          onChange={handleChangeDate}
          maxDate={today}
          KeyboardButtonProps={{
            "aria-label": "change date"
          }}
        />

        <Button
          color="primary"
          variant="contained"
          type="submit"
          onClick={IndivisualForm.handleSubmit}
        >
          Submit
        </Button>
      </Grid>
    </MuiPickersUtilsProvider>
  );
}

Code SandBox Link

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Not sure about why max should work here?

Ideally when you wanted to test out some comparison . They have test method there. So basically you need to do this:

const validationSchema = yup.object().shape({
  date_of_birth: yup
    .date()
    .nullable()
    .notRequired()
    .test(
      "Is date greater",
      "DOB cannot be greater than today's date",
      (value) => {
        if (!value) return true;
        return moment(today).diff(value) > 0;
      }
    )
});

Here is live demo codesandbox: https://codesandbox.io/s/material-demo-forked-kbxlz?file=/demo.js

Or directly see here: https://kbxlz.csb.app/

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!