Skip to main content

Form

A conditionally stateful Form wrapper. This component maintains state and validation of any child FormField components. It utilizes Formik and can receive props related to configuring Formik.

Validation Sync and Async Form and Field level validation is available through the validate prop.

import { Alert } from '@emoney/kyber';

<Alert>
Check out the <a href="#/Tutorials/Intro%20To%20Forms">Intro To Forms Tutorial</a> to get
familiar with how forms work in React with Kyber.
</Alert>;

Basic Example

Wrap the Kyber form field component you would like to use in a FormField and place it inside the Form component. You will need to provide an id and label prop to the FormField. Set the name prop on the field component, this will be used as the key in the returned data.

Result
Loading...
Live Editor

Validation

The Form and related FormField components support multiple types of validation.

The preferred order of validation is:

  1. Form level schema validation with yup.
  2. Form level synchronous validation.
  3. Form level asynchronous validation.
  4. Field level synchronous validation (See FormField).
  5. Field level synchronous validation (See FormField).

Validation Library

Yup is a powerful validation library that can be used with the Form component. There are a lot of options and ways that Yup validation schemas can be constructed. Review the Yup Documentation for full details.

Form Level Schema Validation

Result
Loading...
Live Editor

Sync Form Level Validation

Result
Loading...
Live Editor

Async Form Level Validation

Result
Loading...
Live Editor

Other Validation Uses

Toggling Field Validation

Sometimes it may be necessary to alter form behavior based on local or external state. A common case is that validation changes based on state changes.

You can do this by using Yup's when method to conditionaly add validation based on the value of another field.

Result
Loading...
Live Editor

Cross Field Validation

Result
Loading...
Live Editor

You can also replace the Form's footer by using the footerRenderer prop on the Form. Alternatively you can set this prop to null to disable the footer entirely.

Note: If you add type="submit" to one of your buttons it will be used as the submit button.

Result
Loading...
Live Editor

Further Customization

With children As A Render Prop

See Formik for more details about the props that are forwarded to children.

Result
Loading...
Live Editor

Using Formik Context

It's possible to directly use the Formik context within a subcomponent of a Form. Either as the Formik published hook or through direct access with of the FormikContext object and useContext hook.

Result
Loading...
Live Editor

Using the Form Ref

The Form's ref prop will return a reference to the Formik instance. This gives you access to Formik's props and methods which let you control the form's state manually. For example this can be used to reset the form and clear out the field values.

Resetting the Form

Result
Loading...
Live Editor

Nested Values

Result
Loading...
Live Editor

Analytics

The Form component is trackable through Kyber Analytics. This is the default analytics config.

export default {
value: 'Form',
actions: {
onSubmit: { type: 'FORM_SUBMIT', payload: 'Submit' },
},
};

Props