@@ -91,6 +91,59 @@ const MyComponent = () => {
9191
9292```
9393
94+ ### Usage With Formik
95+
96+ ` react-class-validator ` easily integrates with [ Formik] ( https://formik.org/ ) . You can simply use the ` validate `
97+ function returned from ` useValidation ` , so long as the Formik fields are named the same as the keys in your validation
98+ class. Individual fields will have to be validated with ` onBlur ` functionality.
99+
100+ #### Formik error messages
101+
102+ To display error messages without custom handling, messages will need to be flattened when working with Formik. Do this
103+ by overriding the default ` onErrorMessage ` .
104+
105+ ``` typescript
106+ const options: ValidatorContextOptions = {
107+ onErrorMessage : (error ) => Object .keys (error .constraints )
108+ .map ((accum , key ) => ` ${accum }. ${error .constraints [key ]} ` , ' ' )
109+ };
110+ ```
111+
112+ Then you can simply integrate with the default Formik flow.
113+
114+ ``` typescript jsx
115+ export const Login: FunctionComponent = () => {
116+
117+ const [validate,] = useValidation (LoginValidation );
118+
119+ return (
120+ < Formik initialValues = {{username : ' ' , password : ' ' }}
121+ validateOnBlur
122+ validateOnChange
123+ validate = {validate }>
124+ {({values , errors , touched , handleChange , handleBlur }) = > (
125+ <Form >
126+
127+ < label htmlFor = " username" > Username < / label >
128+ < Field id = " username" name = " username" placeholder = " Username" / >
129+
130+ {errors .username && touched .username ? (
131+ <div >{errors .username }</div >
132+ ) : null }
133+
134+ {/* other fields */ }
135+
136+ < button type = " submit" >
137+ Submit
138+ < / button >
139+
140+ < / Form >
141+ )}
142+ < / Formik >
143+ );
144+ };
145+ ```
146+
94147## Contributors
95148Library built and maintained by [ Robin Schultz] ( http://anigenero.com )
96149
0 commit comments