Skip to content

Form validation errors don't always clear out #268

@triketora

Description

@triketora

I have a validate function that gets called when the submit button is clicked. It returns a object with error messages as per the spec. In my form, I check formApi.errors and render the error messages if they exist. For some reason, formApi.errors doesn't always update after the validate function runs. If I do a console.log inside the validate function, I can see it running and that the errors object is updating, but this doesn't get reflected in the form itself. In the screencap below, at the very end I've changed the field so everything validates and there should be no errors, but the error message still shows. Thoughts?

formapi errors

<Form
  defaultValues={this.state.data}
  validate={this.validate}
  validateOnSubmit={true}
  onSubmit={this.handleSubmit}
  getApi={this.setFormApi}
>
  {formApi =>
    <form
      onSubmit={e => {
        e.preventDefault();
        console.log('submitting');
        return formApi.submitForm(e);
      }}
    >
      <div>
        formApi.submits: {JSON.stringify(formApi.submits)}<br />
        formApi.errors {JSON.stringify(formApi.errors)}
      </div>
      <div className="preferred-name-wrapper">
        <label htmlFor="preferred-name">Preferred name</label>
        <br />
        <Text
          field="preferredName"
          id="preferredName"
          className={`${formApi.errors && formApi.errors.preferredNa
            ? 'has-error'
            : ''}`}
          placeholder="e.g., Goldilocks"
        />
        <p className="helper-text error preferred-name-error">
          {formApi.errors && formApi.errors.preferredName}
        </p>
      </div>
      <div className="email-wrapper">
        <label htmlFor="email">Email</label>
        <br />
        <Text
          field="email"
          id="email"
          className={`${formApi.errors && formApi.errors.email
            ? 'has-error'
            : ''}`}
        />
        <p className="helper-text error email-error">
          {formApi.errors && formApi.errors.email}
        </p>
      </div>
      <div className="password-wrapper">
        <label htmlFor="password">Password</label>
        <br />
        <Text
          field="password"
          id="password"
          type="password"
          className={`${formApi.errors && formApi.errors.password
            ? 'has-error'
            : ''}`}
        />
      </div>
      <div className="password-helper-text-wrapper">
        {(formApi.errors &&
          formApi.errors.password &&
          <p className="helper-text error password-error">
            {formApi.errors && formApi.errors.password}
          </p>) ||
          <p className="helper-text password-helper-text">
            Minimum 8 characters; at least 1 letter and 1 number.
          </p>}
      </div>
      <hr />
      <div className="signup-wrapper">
        <button className="signup bb-yellow" type="submit">
          Sign up
        </button>
        <p className="helper-text error server-error">
          {this.state.serverError}
        </p>
      </div>
    </form>}
</Form>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions