
Good Form implementation for React. Easy drop-in validation. Supports arrays and nested structures. Helps to reduce boilerplate without being opinionated on style. Typescript support.
Installation
npm install react-good-form
Simple Example
Good Form supports basic validations such as email, minLength, maxLength etc. out of the box. It automatically emits onChange events and provides values for fields. onSubmit callback is only triggered when submitting a valid form. Otherwise form will focus to first invalid field.
import { Form } from "react-good-form"
class EmailForm extends React.Component {
state = {
email: ""
}
render() {
return (
<Form
value={this.state}
onChange={person => {
this.setState(person)
}}
onSubmit={() => {
alert(this.state.email)
}}
>
{({ Input }, { invalid, touched }) => (
<div>
<h1>Log in</h1>
<div style={{ color: invalid("email") && touched("email") ? "red" : undefined }}>
<label>Email</label>
<Input email required for="email" />
</div>
<button>OK</button>
</div>
)}
</Form>
)
}
}
Extendable
You can create arbitary rules by providing a rule function.
const rule = email => email.endsWith("hotmail.com")
<Input
rule={rule}
email={true}
for="email"
/>
Or an regular expression.
const regExp = /(123456|password)/
<Input
type="password"
regExp={regExp}
for="password"
/>
Supports nested structures
Provide paths to nested structures as arrays.
<div style={{ color: invalid(["address", "street"]) && touched(["address", "street"]) && "red" }}>
<label>Street</label>
<Input for={["address", "street"]} minLength={5} maxLength={100} />
</div>
Documentation
https://osaario.github.io/react-good-form/
Type aliases
BrokenRules
BrokenRules: object & object & object
CheckboxRules
CheckboxRules: object
FormEventType
FormEventType: object | object | object | object | object
FormGroupAddons
FormGroupAddons: object
Type declaration
-
children: React.ReactNode
-
Optional errorForBrokenRules?: undefined | function
-
Optional leftAddon?: React.ReactNode
-
Optional rightAddon?: React.ReactNode
FunctionRule
FunctionRule: function
Type declaration
-
- (value: string | null | undefined): boolean
-
Parameters
-
value: string | null | undefined
Returns boolean
InputProps
InputProps: _.Omit<React.DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>, "ref"> & I extends "number" | "price" ? NumberInputRules : I extends "checkbox" ? CheckboxRules : StringRules & object & object
InputType
InputType: "text" | "email" | "number" | "checkbox" | "password" | "price" | undefined
LensPathType
LensPathType: LensPathType<T, A, U, S, K>
NumberFunctionRule
NumberFunctionRule: function
Type declaration
-
- (value: number | null | undefined): boolean
-
Parameters
-
value: number | null | undefined
Returns boolean
NumberInputRules
NumberInputRules: object
SelectProps
SelectProps: _.Omit<React.DetailedHTMLProps<InputHTMLAttributes<HTMLSelectElement>, HTMLSelectElement>, "ref"> & object
StringRules
StringRules: object
TextAreaProps
Text
AreaProps: _.Omit<React.DetailedHTMLProps<TextareaHTMLAttributes<HTMLTextAreaElement>, HTMLTextAreaElement>, "ref"> & StringRules & object
ValidationProps
ValidationProps: object
Type declaration
-
children: function
-
- (fieldInteractionState: object): Element
-
Parameters
-
fieldInteractionState: object
-
dirty: boolean
-
-
pristine: boolean
-
touched: boolean
-
untouched: boolean
-
valid: boolean
Returns Element
-
ValidationRuleType
ValidationRuleType: function
Type declaration
-
- (value: number | string | boolean | null | undefined, ruleValue: T): BrokenRule | null
-
Parameters
-
value: number | string | boolean | null | undefined
-
ruleValue: T
Variables
Const L
L: any = require('partial.lenses')
Const defaultLocale
defaultLocale: "en" = "en"
Const emailRegex
emailRegex: RegExp = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/
Const omitFromInputs
omitFromInputs: string[] = ['ref', 'for', 'leftAddon', 'rightAddon'].concat(Object.keys(formRules)).concat(Object.keys(numberRules)).concat(Object.keys(checkBoxRules))
Const wrappedFields
wrappedFields: any = L.compose(wrappedFieldsOrPrimitives,L.when(isWrappedValue))
Const wrappedFieldsOrPrimitives
wrappedFieldsOrPrimitives: any = L.lazy((rec: any) => {return L.ifElse(isObject, L.ifElse(isWrappedValue, L.optional, [L.children, rec]), L.optional)})
Const wrappedTypeName
wrappedTypeName: "oTMiaY58D7" = "oTMiaY58D7"