Docs

Reacting to Form State Changes

How to recognize different states of fields and forms and react accordingly.

You can display some parts of the form differently, depending on the form state. For example, you can disable the Submit button when the form has validation errors, or show an 'operation in progress' spinner while an async form submission is in progress.

export default function ProfileView() {

    const form = useForm(EntityModel);

    return (
      <>
        ...
        <Button disabled={form.invalid}>submit</Button>
      </>
    );
}

The following properties are available both for the form as a whole, and for each field independently. They are a part of the BinderNode interface. You can access form status properties for the whole form from the UseFormResult instance returned from useForm:

const form = useForm(EntityModel);

const invalid = form.invalid;
const dirty = form.dirty;
const errors = form.errors;
...

To access form status properties for individual fields, use the UseFormPartResult instance returned from useFormPart:

const form = useForm(EntityModel);
const nameField = useFormPart(form.model.name);

const invalid = nameField.invalid;
const dirty = nameField.dirty;
const errors = nameField.errors;
  • dirty: true if the value of the form or field has been modified by the user

  • visited: true if the form or field has been focused by the user

  • invalid: true if the form or field has validation errors

  • required: true if the form or field is required

  • errors: a list of all validation errors for a form or field and its sub-fields

  • ownErrors: a list of all the validation errors for a field (without sub-fields) or for the form (not specific to any field)

The following properties are available for the form as a whole, but not for individual fields. They are a part of the Binder interface. For example, you can access these properties via binder.validating.

  • validating: true if the form is performing some validation

  • submitting: true if the form is submitting the data to a callback

Example: Disable the Form While Submission Is in Progress

If form submission could take a long time, it’s usually a good idea to give users a quick visual indication that something is happening. You may also want to prevent more form submissions until the first one is completed (for example, in a payment form).

With the TypeScript Binder API, this can be done using the submitting property. In the following example, binder.submitting is bound to the disabled property of the Submit button to disable repeating form submissions. It’s also used as a condition to render an additional 'submitting' message.

export default function ProfileView() {

    const {model, submit, field, invalid, submitting} = useForm(PersonModel, {
        onSubmit: async (e) => {
          await PersonEndpoint.sendEntity(e);
        }
      });

    return (
      <>
        <VerticalLayout theme="spacing padding">
          <TextField label="First name" {...field(model.firstName)}></TextField>
          <TextField label="Last name" {...field(model.lastName)}></TextField>
        </VerticalLayout>
        <HorizontalLayout theme="spacing padding">
          <Button theme="primary" onClick={submit} disabled={invalid || submitting}>Save</Button>
          <span className="label" style={{visibility: submitting ? 'visible' : 'hidden' }}>submitting</span>
          <div className="spinner" style={{visibility: submitting ? 'visible' : 'hidden' }}></div>
          </HorizontalLayout>
      </>
    );
}
Disable the form while submission is in progress

Example: List All Validation Errors if Validation Fails

Sometimes you may want to list all validation errors in one place. This is convenient especially in large forms, where it can be difficult to find that one field that failed the validation.

With the TypeScript Binder API, this can be done using the errors property. In the following example, the form template iterates over the form.errors list and renders the full list under the form.

<dl>
  {form.errors.map(error => (
      <>
        <dt>{error.property as string}</dt>
        <dt>{error.message as string}</dt>
      </>
  ))}
</dl>
Disable the form while submission is in progress