Skip to content

Pristine with form array fields #151

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

Closed
Gi972 opened this issue Feb 9, 2018 · 13 comments
Closed

Pristine with form array fields #151

Gi972 opened this issue Feb 9, 2018 · 13 comments
Labels

Comments

@Gi972
Copy link

Gi972 commented Feb 9, 2018

Are you submitting a bug report or a feature request?

I report a bug

What is the current behavior?

I use the form array field with a initialized value, when I change the value (in the example, I put "toto", next "tot" and back with "toto") so I come back on the initial value of the select component but
the pristine doesn't come back at the initial value.

I Fork the field Array example here:

https://codesandbox.io/s/j7o525yn23

Thanks @erikras

@zsolt-dev
Copy link

I have the same issue. Is there a way to fix this?

@snhasani
Copy link

You have to pass initial state to reset.

<ActionButton
    type="button"
    onClick={() => reset(INITIAL_VALUES)}
    disabled={submitting || pristine}
>

https://github.com/final-form/final-form#reset-initialvalues-object--void

@fernandomg
Copy link

@snhasani the issue is not related to that.

It's expected for a field to recover its pristine status if (after changing the value) the value matches its initial value.

You can read a better explanation here: final-form/final-form#55 (comment)

I updated the dependencies for the original code that @Gi972 posted, and it's still happening: https://codesandbox.io/s/x99z5mzn6w

@erikras
Copy link
Member

erikras commented Jun 1, 2018

Confirmed. This is a bug.

@erikras erikras added the bug label Jun 1, 2018
@311chaos
Copy link

I opened a ticket on react-final-form-arrays that looks to be quite similar.

@erikras - Let me know if I should close my ticket there as a duplicate (not sure if its an issue with rff or rffa).

final-form/react-final-form-arrays#34

@bogdansoare
Copy link

also having this issue

@bogdansoare
Copy link

this issue also happens when you have keepDirtyOnReinitialize on the form and there is a rerender
https://codesandbox.io/s/q9r4r2o2k6

@maciejmyslinski
Copy link
Contributor

I'm on it

@maciejmyslinski
Copy link
Contributor

update: debugged. I'll describe it on the first @Gi972 example.

It's because react-final-form-arrays registers three fields: customers, customers[0].firstName and customers[0].lastName.

Alt text

This is problematic, because later final-form iterate over all fields and compare their initial values to current values using triple equals.

Since customers value is an array, it never equals default values, since [] !== [].

Alt text

@maciejmyslinski
Copy link
Contributor

from what I understand, registering customers field is desired.

I see 3 possible solutions:

  1. Provide custom isEqual function for customers field that always returns true. This would mean that we only calculate if a form is pristine checking values for all registered fields in the array. I'm not sure if this would behavie correcly when a field is being removed.
  2. Provide custom isEqual function for customer field that runs deep equality checking.
  3. Update final-form function that calculates pristine so it handles objects/arrays correctly - either by ommiting them or deeply checking.

@maciejmyslinski
Copy link
Contributor

ok, I wrote tests and solved the issue in this PR ☝️
I appreciate any suggestions/code reviews 🙂

@erikras
Copy link
Member

erikras commented Oct 16, 2018

Published fix in [email protected].

@lock
Copy link

lock bot commented Oct 16, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 16, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

8 participants