Form
Overview
Form component renders a value to be updated and validated by child Fields.
Forms can be thought of as <input/>s for complex values, or models. A Form aggregates
a bunch of smaller inputs, each in charge of updating a small part of the overall model.
The Form will integrate and validate each change and fire a single unified onChange with the new value.
Validation errors can be displayed anywhere inside a Form with Message Components.
Schema
A schema is not strictly required, however describing form data with a schema offers many benefits that make their use worthwhile.
- Schema provide an expressive language for describing validation at a field level as well as form-wide tests.
- Schema provide type metadata that React Formal can use to simplify configuration.
yup.array()fields are automatically render<select multiple>,yup.boolean()s render checkboxes, and so on - Schema allow encapsulating server serialization and business logic apart from your specific UI. Validation and transformations are not strictly tied to form input layout or UI means more maintainable, composable, and testable code.
Field Inference
A Field's schema provides runtime type information that can be used to help reduce boilerplate UI code. Field's will render the most appropriate native input for your field type (you can always override it).
Data Serialization
Schema can also do data sanitiation and transformation. When a Form is submitted
the current value is passed through
the schema a final time as a whole, the transformed value is what is passed back
to the onSubmit handlers (you can override this behavior with strict).
API
import { Form } from 'react-formal';abortEarly
Controls how errors are dealt with for field level validation. When set, the first validation error a field throws is returned instead of waiting for all validations to finish
as
A tag name or Component class the Form should render.
If null are false the form will simply render it's child. In
this instance there must only be one child.
'form'children
className
context
yup schema context
debug
toggle debug mode, which console.warns validation errors
defaultErrors
ErrorUtils.EMPTY_ERRORSdefaultTouched
{}defaultValue
delay
Time in milliseconds that validations should be debounced. Reduces the amount of validation calls made at the expense of a slight delay. Helpful for performance.
300errors
An object hash of field errors for the form. The object should be keyed with paths
with the values being an array of errors or message objects. Errors can be
left uncontrolled (use defaultErrors to set an initial value)
or managed along with the onError callback. You can use any object shape you'd like for
errors, as long as you provide the Form.Message component an extract prop that
understands how to pull out the strings message. By default it understands strings and objects
with a 'message' property.
<Form errors={{ "name.first": [ 'First names are required', { message: "Names must be at least 2 characters long", type: 'min' } ],}}/>getter
A value getter function. getter is called with path and value and
should return the plain javascript value at the path.
function( path: string, value: any,): ObjectformGetternoValidate
Turns off input validation for the Form, value updates will continue to work.
onBeforeSubmit
Callback that is fired in response to a submit, before validation runs.
function onSubmit(formValue) { // do something with valid value}onChange
Callback that is called when the value prop changes.
function ( value: any, updatedPaths: string[])onError
Callback that is called when a validation error occurs. It is called with an errors object
onInvalidSubmit
Callback that is fired when the native onSubmit event is triggered. Only relevant when
the component prop renders a <form/> tag. onInvalidSubmit will trigger only if the form is invalid.
function onInvalidSubmit(errors){ // do something with errors}onReset
Callback that is fired in response to a form reset. onReset fires before
the accompanying onChange.
function onReset() { // reset has been called}onSubmit
Callback that is fired in response to a submit, after validation runs for the entire form.
function onSubmit(formValue) { // do something with valid value}onSubmitFinished
onTouch
Callback that is called when a field is touched. It is called with an touched object
onValidate
Callback that is called whenever a validation is triggered. It is called before the validation is actually run.
function onValidate(event) { let { type, fields, args } = event}schema
A Yup schema that validates the Form value prop. Used to validate the form input values
For more information about the yup api check out: https://github.com/jquense/yup/blob/master/README.md
setter
A value setter function. setter is called with path, the form value and the path value.
The setter must return updated form value, which allows you to leave the original value unmutated.
The default implementation uses the react immutability helpers,
letting you treat the form value as immutable.
function( path: string, formValue: any, pathValue: any): ObjectformSetterstrict
Validations will be strict, making no attempt to coarce input values to the appropriate type.
falsestripUnknown
submitForm
touched
An object hash of field paths and whether they have been "touched" yet
value
Form value object, can be left uncontrolled;
use the defaultValue prop to initialize an uncontrolled form.