<OneForm>
The center of everything, the one and only, OneForm.
Last updated
The center of everything, the one and only, OneForm.
Last updated
children
Can be any valid React element (example: single or multiple components and text).
OneForm
provides no render props to children; therefore, you shouldn't pass in an anonymous function.
This is the only required prop in OneForm.
Without it, OneForm would only render an HTML <form />
and provide literally no other.
errorMessages
This is an object where each prop contains an array of strings where the strings are error messages tied to specific fields.
Updating this value will wipe all errors from the form and only display the ones you passed in.
If you want to merge new errors into OneForm, use updatedErrorMessages
instead.
These exports have access to error messages defined by errorMessages
:
Field
FieldErrorMessage
useField
useFieldErrorMessages
groupValidations
An array of objects containing a getErrorMessages
function that gets called when any subscribed value changes.
When returning a value, instead of returning a boolean, you need to specify which fields actually errored and the message for each of those fields. In the above example, we returned an error message on only the lastName
field.
hasFieldChangeValidation
This value is true
by default.
onChange
Pass in a callback function to onChange
, and it'll be called anytime a field changes.
This will give you:
Returning a values object from onChange
let's you update the value of any field in the form.
For example, this can be used to update the value of a domain name from .com
to .org
when selecting from a dropdown.
onSubmit
Pass a callback to onSubmit
and receive:
updatedErrorMessages
This is an object where each prop contains an array of strings where the strings are error messages tied to specific fields.
Updating this value will only overwrite previous error message keys if you pass them to updatedErrorMessages
.
If you want to replace all errors, use errorMessages
instead.
These exports have access to error messages defined by updatedErrorMessages
:
Field
FieldErrorMessage
useField
useFieldErrorMessages
updatedValues
An object of field name keys and string values.
You can use any value, it doesn't have to be a string, but if you want to display the value in any HTML input field, it needs to be a string.
updatedValues
is great when receiving async or realtime data.
This prop is extremely important if you're using onChange
to save to your database and need to return any changed or updated values back to OneForm.
If you're wanting to update field values synchronously, like changing the .com
to .org
in a domain name, you'll want to use onChange
instead.
If you want to initialize values in your form, it might be better to use values
instead.
validations
An object field names. getIsValid
is called whenever that field changes.
The errorMessage
is set whenever getIsValid
returns false
. When it's set to true
, the errorMessage
is removed.
It's also possible to configure your form to validate only on submit:
values
An object of field name keys and string values.
You can use any value, it doesn't have to be a string, but if you want to display the value in any HTML input field, it needs to be a string.
values
overwrites all values in OneForm with this new object. This is typically named initial values.
OneForm lets you pass changes to values
as many times as you want.
To reset all form values after submitting successfully, just pass {}
to values:
Prop Name
Prop Type
Description
children
Node
errorMessages
Object
groupValidations
Array
hasFieldChangeValidation
Boolean
Default value (true
)
onChange
Function
onSubmit
Function
updatedErrorMessages
Object
updatedValues
Object
validations
Object
values
Object
Prop Name
Description
fieldName
Name of the field that changed.
value
Value of the field that changed.
values
All field values in a shallow object.
Prop Name
Description
allValues
Every field that's ever had a value. This is not what you want unless you have multipart forms.
registeredValues
An object only containing fields that are registered. This almost always means fields which are currently mounted.
This is the value most will want.