Formik: [v2] Regression in validation of FieldArray after change

Created on 28 Oct 2019  路  8Comments  路  Source: formium/formik

馃悰 Bug report

Current Behavior

Validation is not run after I push an item to the array.

Expected behavior

In version 1.x, validation was run right away.

Reproducible example

https://codesandbox.io/s/formik-v2-fieldarray-validation-repro-hrqqj

The repro is using Formik v2. You will see that adding new items by clicking the button doesn't trigger validation.

If you downgrade to Formik 1.x you will see that adding an item will trigger validation.

Suggested solution(s)

Revert to the previous behavior or document the breaking change.

Most helpful comment

@jaredpalmer the behavior has improved in 2.1.1 but it's not the same as it was in 1.x.

See the updated sandbox here: https://codesandbox.io/s/formik-v2-fieldarray-validation-repro-hrqqj

And the screenshots below. Notice that the 2.1.1 version is "falling one field behind". The when I add a new field to the array the previous ones are validated but not the newly added one, whereas in version 1.5.8 the form state was in sync and also the newly added field is being validated at the time it's added.

I would reopen this issue.

1.5.8

formik-v2-fieldarray-validation-repro - CodeSandbox - Edited

2.1.1

formik-v2-fieldarray-validation-repro - CodeSandbox - Edited (1)

All 8 comments

Here's another reproducible example using the validate prop: https://codesandbox.io/s/ecstatic-swanson-tz1ih

It looks like the issue is that the second argument to setFormikState (the callback) is never called. https://github.com/jaredpalmer/formik/blob/master/src/FieldArray.tsx#L169

This is because setFormikState only takes one argument. https://github.com/jaredpalmer/formik/blob/master/src/Formik.tsx#L664.

The following test fails:

    it('should validate form after adding the value', async () => {
      let arrayHelpers: any;
      let validate = jest.fn();
      let form = (
        <TestForm validate={validate}>
          <FieldArray
            name="friends"
            render={arrayProps => {
              arrayHelpers = arrayProps;
              return null;
            }}
          />
        </TestForm>
      );

      let { rerender } = render(form);
      arrayHelpers.push('tom');
      rerender(form);
      await wait(() => {
        expect(validate).toHaveBeenCalled();
      });
    });

I forked this repo at https://github.com/nbycomp/formik and added the failing test.

why not submit the failing test as a PR?

I'm experiencing the same issue with validation not running after calling replace on an existing item in the field array.

I forked off your example, @simoneb
https://codesandbox.io/s/formik-v2-fieldarray-validation-repro-eeylg

I believe this is fixed in 2.1

@jaredpalmer the behavior has improved in 2.1.1 but it's not the same as it was in 1.x.

See the updated sandbox here: https://codesandbox.io/s/formik-v2-fieldarray-validation-repro-hrqqj

And the screenshots below. Notice that the 2.1.1 version is "falling one field behind". The when I add a new field to the array the previous ones are validated but not the newly added one, whereas in version 1.5.8 the form state was in sync and also the newly added field is being validated at the time it's added.

I would reopen this issue.

1.5.8

formik-v2-fieldarray-validation-repro - CodeSandbox - Edited

2.1.1

formik-v2-fieldarray-validation-repro - CodeSandbox - Edited (1)

Looks like the validation is only triggering for push, but not for replace and remove

@simoneb Any updates?.. The issue is still reproducible

@pavelspichonak it is still reproducible in 2.1.5. When a new field is added to a field array, its value is not validated. The behavior is the same as in 2.1.1. See screenshot below and updated sandbox.

React App

Was this page helpful?
0 / 5 - 0 ratings

Related issues

outaTiME picture outaTiME  路  3Comments

sibelius picture sibelius  路  3Comments

Jungwoo-An picture Jungwoo-An  路  3Comments

dearcodes picture dearcodes  路  3Comments

ancashoria picture ancashoria  路  3Comments