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

Form.Item doesn't show error for array validation onSubmit #193

Open
vibegame opened this issue Nov 28, 2021 · 1 comment
Open

Form.Item doesn't show error for array validation onSubmit #193

vibegame opened this issue Nov 28, 2021 · 1 comment

Comments

@vibegame
Copy link

Hello everyone! ✋

I'm using validationSchema for Formik component and using Form.Item to display validation errors.

Code:

const options: any[] = [];

const validationSchema = Yup.object({
  items: Yup.array().min(1)
});

const initialValues = {
  items: []
};

const onSubmit = () => {};

function App() {
  return (
    <Formik validationSchema={validationSchema} initialValues={initialValues} onSubmit={onSubmit}>
      <Form>
        <Form.Item name="items">
          <Select name="items" mode="multiple" options={options} />
        </Form.Item>
        <SubmitButton>Submit</SubmitButton>
      </Form>
    </Formik>
  );
}

How to reproduce
Copy and paste the code above. Run the project and click on button Submit.

Actual Result
Error is not displayed

Expected Result
The error is displayed under Select: "items field must have at least 1 items"

Description
Form.Item doesn't show the error, because it sets "touched" to false. It handles "touched" incorrectly when it is an empty array.
I researched this problem and understood why this is happening:
If the touched field is an empty array, it means that the field is touched. jaredpalmer/formik#3344 (comment)

The Problem
The following code incorrectly handles an empty touched array -> Form.Item: 28. If a touched array is empty, then it sets isTouched to false.

Thanks for the answers and help!

@sirkeng
Copy link

sirkeng commented Feb 23, 2022

I also have this problem

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

No branches or pull requests

2 participants