Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validationSchema not working after page reload #3989

Open
sgarchavada opened this issue Aug 31, 2024 · 1 comment
Open

validationSchema not working after page reload #3989

sgarchavada opened this issue Aug 31, 2024 · 1 comment

Comments

@sgarchavada
Copy link

sgarchavada commented Aug 31, 2024

Bug report

Current Behavior

I am just simply adding max value validation, which is working fine for the first time, but when I reload the page, and typing again, validation is not applying.

Expected behavior

validation should work after the page reload also.

Reproducible example

import { Formik } from 'formik';
import * as Yup from 'yup';
import TextInput from '../common/TextInput';

const DebitCard = () => {
  const validateSchema = Yup.object().shape({
    cardNo: Yup.string()
      .max(5, 'Must be exactly 5 characters')
      .required('Required')
  })

  return (
    <>
      <Formik
        initialValues={{
          cardNo: '',
        }}
        validationSchema={validateSchema}
        onSubmit={values => {
          alert(JSON.stringify(values, null, 2));
        }}
      >
          <form >
            <div className="flex space-x-5 mt-3">
              <TextInput
                label="Card Number"
                name="cardNo"
                type="number"
               />
          </form>
      </Formik>
    </>
  )
}

export default DebitCard;

Suggested solution(s)

Additional context

It only happen for the first time, if I unfocus the input it started showing the validation error, second time its working fine without loosing focus on input

Your environment

Software Version(s)
Formik ^2.4.6
React ^1
TypeScript ^5
Browser Chrome
npm/Yarn npm
Operating System Window 11
@erlanggaht
Copy link

erlanggaht commented Nov 26, 2024

try to use the component given directly from the formik. the code above I tried to correct

"use client";

import { ErrorMessage, Field, Form, Formik } from "formik";
import * as Yup from "yup";
// import TextInput from "../common/TextInput";

const DebitCard = () => {
 const validateSchema = Yup.object().shape({
   cardNo: Yup.string()
     .max(5, "Must be exactly 5 characters")
     .required("Required"),
 });

 return (
   <>
     <Formik
       initialValues={{
         cardNo: "",
       }}
       validationSchema={validateSchema}
       onSubmit={(values) => {
         alert(JSON.stringify(values, null, 2));
       }}
       action="#"
     >
       <Form>
         <div className="flex space-x-5 mt-3">
         <Field type="number" name="cardNo" placeholder="Number" />{" "}
         </div>
         <ErrorMessage name="cardNo" />
       </Form>
     </Formik>
   </>
 );
};

export default DebitCard;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants