@@ -10,7 +10,6 @@ import {
10
10
ErrorMap ,
11
11
FormError ,
12
12
} from "typed-react-form" ;
13
- import tv from "typed-object-validator" ;
14
13
15
14
function MyForm ( ) {
16
15
const form = useForm ( { email : "" } ) ;
@@ -235,8 +234,38 @@ export function YupFormExample() {
235
234
< FormError form = { form } name = "email" />
236
235
< FormInput form = { form } name = "password" type = "password" />
237
236
< FormError form = { form } name = "password" />
238
- { /* Listen for any change on the form, and disable the submit button when there is an error */ }
239
- < AnyListener form = { form } render = { ( form ) => < button disabled = { form . error } > Submit</ button > } />
237
+ < button type = "submit" > Submit</ button >
238
+ </ form >
239
+ ) ;
240
+ }
241
+
242
+ import tv , { SchemaType } from "typed-object-validator" ;
243
+
244
+ const RegisterRequestSchema = tv . object ( {
245
+ email : tv . email ( "Please enter a valid email address." ) . doCase ( "lower" ) . min ( 1 , "Please enter an email" ) ,
246
+ password : tv . string ( ) . min ( 1 , "Please enter a password" ) ,
247
+ } ) ;
248
+ type RegisterRequest = SchemaType < typeof RegisterRequestSchema > ;
249
+
250
+ export function TypedValidationExample ( ) {
251
+ const form = useForm < RegisterRequest > (
252
+ { email : "" , password : "" } ,
253
+ ( values ) => RegisterRequestSchema . validate ( values ) ,
254
+ true
255
+ ) ;
256
+
257
+ function submit ( ) {
258
+ // Transform if you want (will lowercase email in this case)
259
+ console . log ( "submit" , form . values , RegisterRequestSchema . transform ( form . values ) ) ;
260
+ }
261
+
262
+ return (
263
+ < form onSubmit = { form . handleSubmit ( submit ) } >
264
+ < FormInput form = { form } name = "email" type = "email" />
265
+ < FormError form = { form } name = "email" />
266
+ < FormInput form = { form } name = "password" type = "password" />
267
+ < FormError form = { form } name = "password" />
268
+ < button type = "submit" > Submit</ button >
240
269
</ form >
241
270
) ;
242
271
}
@@ -257,6 +286,8 @@ export default function Testing() {
257
286
< ValidationExample />
258
287
< hr />
259
288
< YupFormExample />
289
+ < hr />
290
+ < TypedValidationExample />
260
291
</ >
261
292
) ;
262
293
}
0 commit comments