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

canSubmit in form is true even if some fields are unvalid #358

Closed
arnars opened this issue Nov 19, 2019 · 9 comments
Closed

canSubmit in form is true even if some fields are unvalid #358

arnars opened this issue Nov 19, 2019 · 9 comments
Labels
legacy Issues or pull requests relating to the legacy version of the forms library

Comments

@arnars
Copy link

arnars commented Nov 19, 2019

In your own example: https://codesandbox.io/s/react-form-custom-select-multi-select-inputs-6962t

The canSubmit is true even before the two colors in the multiselect have been set. As long as the single select have been set, it's all good.

Is this expected?

@arnars arnars changed the title canSubmit in form is true even all fields are unvalid canSubmit in form is true even if some fields are unvalid Nov 19, 2019
@tannerlinsley
Copy link
Collaborator

tannerlinsley commented Nov 20, 2019 via email

@AndriiUhryn
Copy link

This is an issue for me as well, this needs to be fixed ASAP. Thanks.

@AndriiUhryn
Copy link

Any fix for this?? My buttons are all active even if an error occurred after validation, not healthy. Please fix this.

@androsj
Copy link

androsj commented Jul 15, 2020

Hi, experiencing issues with this as well. I was expecting isValid to remain false until all fields were valid (using fields with validatePristine enabled). I noticed it seems to return true just on the 2nd render. All other renders receive the expected value (false).

Edit: I took a closer look... this is how it behaves:

  1. field is validating
  2. field is done validating and no error is set, but it should be! Form now says its valid: !fieldsAreValidating && fieldsAreValid && !meta.error
  3. field now shows an error

@crutchcorn crutchcorn added the legacy Issues or pull requests relating to the legacy version of the forms library label Aug 29, 2023
@manavm1990
Copy link

As from today, it's still an issue. Essentially canSubmit lies 🤥 . I have resolved from my side by doing like this 1️⃣ :

<div className="mt-5 place-self-center md:col-span-2">
          <formAPI.Subscribe
            selector={(state) => state}
            children={(state) => (
              <SubmitButton isValid={validateFieldMeta(state.fieldMeta)} />
            )}
          />

And, here is my utility function to do the full validation before 'activating' SubmitButton:

import { type FieldMeta } from '@tanstack/react-form';

export function validateFieldMeta(
  fieldMeta: Record<string, FieldMeta>,
): boolean {
  return Object.values(fieldMeta).every(({ isTouched, touchedErrors }) => {
    return isTouched && touchedErrors.length === 0;
  });
}

And, some tests ✅ for the same:

describe('validateFieldMeta', () => {
  it('returns false if all fields are not touched', () => {
    const input = {
      name: {
        isValidating: false,
        isTouched: true,
        touchedErrors: [],
        errors: [],
        errorMap: {},
      },
      email: {
        isValidating: false,
        isTouched: true,
        touchedErrors: [],
        errors: [],
        errorMap: {},
      },
      help: {
        isValidating: false,
        isTouched: false,
        touchedErrors: [],
        errors: [],
        errorMap: {},
      },
    };

    const expected = false;

    const actual = validateFieldMeta(input);

    expect(actual).toBe(expected);
  });

  it('returns false if all fields are touched but at least one has errors', () => {
    const input = {
      name: {
        isValidating: false,
        isTouched: true,
        touchedErrors: [],
        errors: [],
        errorMap: {},
      },
      email: {
        isValidating: false,
        isTouched: true,
        touchedErrors: [],
        errors: [],
        errorMap: {},
      },
      help: {
        isValidating: false,
        isTouched: true,
        touchedErrors: ['Some error'], // Field with error
        errors: [],
        errorMap: {},
      },
    };

    const expected = false;
    const actual = validateFieldMeta(input);
    expect(actual).toBe(expected);
  });

  it('returns true if all fields are touched and have no errors', () => {
    const input = {
      name: {
        isValidating: false,
        isTouched: true,
        touchedErrors: [],
        errors: [],
        errorMap: {},
      },
      email: {
        isValidating: false,
        isTouched: true,
        touchedErrors: [],
        errors: [],
        errorMap: {},
      },
      help: {
        isValidating: false,
        isTouched: true,
        touchedErrors: [],
        errors: [],
        errorMap: {},
      },
    };

    const expected = true;
    const actual = validateFieldMeta(input);
    expect(actual).toBe(expected);
  });
});

Only minor issue now is that whenever we use the browser's autocomplete features, without actually clickng 🐭 and typing ⌨️ it doesn't register as being isTouched.

@t-rosa
Copy link
Contributor

t-rosa commented Mar 27, 2024

When first rendered, the onUpdate function is called and even if the default state canSubmit is set to false, the statement (image below) is always true and sets canSubmit to true :
image

@crutchcorn
Copy link
Member

crutchcorn commented Mar 27, 2024

@t-rosa @manavm1990 please open a new issue with a StackBlitz link or similar. This ticket is specific for the legacy codebase, not the new codebase you're reporting an issue on

@engelkes-finstreet
Copy link

@crutchcorn I added a new issue here with a StackBlitz showing the behaviour for the new Codebase.

@crutchcorn
Copy link
Member

Hey all, I'm closing this issue as it appears to be related to a version of the old non-current @tanstack/react-form and friend APIs. We're looking to 1.x soon enough, and I want to more easily filter down to more recent issues.

Thanks for being early adopters of our tooling! We know that it can lead to issues like this being unresolved sometimes, but we're working hard on the new iteration of Form.

Maybe take a look and let us know what you think?

https://tanstack.com/form

Best,

  • Corbin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legacy Issues or pull requests relating to the legacy version of the forms library
Projects
None yet
Development

No branches or pull requests

8 participants