@@ -2,7 +2,7 @@ import { FunctionComponent, useState } from "react";
2
2
import Link from "next/link" ;
3
3
import { useRouter } from "next/router" ;
4
4
import Head from "next/head" ;
5
- import { Formik } from "formik" ;
5
+ import { ErrorMessage , Formik } from "formik" ;
6
6
import { fetch } from "../../utils/dataAccess" ;
7
7
import { { { { ucf } } } } from '../../types/{{{ucf}}}' ;
8
8
@@ -21,7 +21,7 @@ export const Form: FunctionComponent<Props> = ({ {{{lc}}} }) => {
21
21
await fetch ( { { { lc} } } [ '@id' ] , { method : "DELETE" } ) ;
22
22
router . push ( "/{{{name}}}" ) ;
23
23
} catch ( error ) {
24
- setError ( " Error when deleting the resource." ) ;
24
+ setError ( ` Error when deleting the resource: ${ error } ` ) ;
25
25
console . error ( error ) ;
26
26
}
27
27
} ;
@@ -42,7 +42,7 @@ export const Form: FunctionComponent<Props> = ({ {{{lc}}} }) => {
42
42
// add your validation logic here
43
43
return errors ;
44
44
} }
45
- onSubmit = { async ( values , { setSubmitting, setStatus } ) => {
45
+ onSubmit = { async ( values , { setSubmitting, setStatus, setErrors } ) => {
46
46
const isCreation = ! values [ "@id" ] ;
47
47
try {
48
48
await fetch ( isCreation ? "/{{{name}}}" : values [ "@id" ] , {
@@ -57,8 +57,9 @@ export const Form: FunctionComponent<Props> = ({ {{{lc}}} }) => {
57
57
} catch ( error ) {
58
58
setStatus ( {
59
59
isValid : false ,
60
- msg : `Error when ${ isCreation ? 'creating' : 'updating' } the resource. ` ,
60
+ msg : `${ error . defaultErrorMsg } ` ,
61
61
} ) ;
62
+ setErrors ( error . fields ) ;
62
63
}
63
64
setSubmitting ( false ) ;
64
65
} }
@@ -91,7 +92,11 @@ export const Form: FunctionComponent<Props> = ({ {{{lc}}} }) => {
91
92
onBlur = { handleBlur }
92
93
/>
93
94
</ div >
94
- { errors . { { name } } && touched . { { name} } && < div className = "invalid-feedback" > { errors . { { name } } } </ div > }
95
+ < ErrorMessage
96
+ className = "text-danger"
97
+ component = "div"
98
+ name = "{{name}}"
99
+ />
95
100
{ { / each} }
96
101
97
102
{ status && status . msg && (
@@ -105,12 +110,6 @@ export const Form: FunctionComponent<Props> = ({ {{{lc}}} }) => {
105
110
</ div >
106
111
) }
107
112
108
- { error && (
109
- < div className = "alert alert-danger" role = "alert" >
110
- { error }
111
- </ div >
112
- ) }
113
-
114
113
< button
115
114
type = "submit"
116
115
className = "btn btn-success"
0 commit comments