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

SubmitButton is enabled despite failing validation so long as it's on initial values #146

Open
yoruvo opened this issue May 27, 2020 · 1 comment

Comments

@yoruvo
Copy link

yoruvo commented May 27, 2020

Possible values in form:

export interface SupportFormValues {
  name: string
  email: string
  topic: string
  version?: string
  phone?: string
  description: string
}

Initial values:

const softwareVersion = "1.10.0"

export const initialValues = {
  name: "",
  email: "",
  topic: "error",
  version: softwareVersion,
  phone: "",
  description: "",
}

Validation schema:

  name: Yup.string().required("form.validation.required"),

  email: Yup.string()
    .email("form.validation.emailInvalid")
    .required("form.validation.required"),

  topic: Yup.string()
    .required("form.validation.required")
    .oneOf(["general", "error", "call"], "form.validation.topicInvalid"),

  version: Yup.string().when("topic", {
    is: (topic) => topic === "error",
    then: Yup.string()
      .required("form.validation.required")
      .matches(versionRegExp, "form.validation.versionInvalid"),
  }),

  phone: Yup.string().when("topic", {
    is: (topic) => topic === "call",
    then: Yup.string()
      .required("form.validation.required")
      .matches(phoneRegExp, "form.validation.phoneInvalid"),
  }),

  description: Yup.string()
    .required("form.validation.required")
    .min(10, "form.validation.descriptionLength"),
})

As you can see, all the fields are required, but many are empty.

Yet, at the start of the form, and whenever you reset to the "initial values" (Version at 1.10.0, Topic at "error", rest empty), the Submit Button is enabled despite not passing validation.

@jannikbuschke
Copy link
Owner

This is by design (https://github.com/jannikbuschke/formik-antd#submitting-and-resetting-forms).

If the button would be disabled, the user might not see any validation feedback, as it is usually only rendered if the corresponding field has been touched. By enabling the submit button and clicking it, all fields will be touched, thus rendering the feedback if the form is invalid. You might want to run validation again in your submit handler, if you don't want to send an invalid form to your backend (there you should validate anyway).

See also #135, #125, #87, #73

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