Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

284
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda