@@ -4,10 +4,18 @@ import {
4
4
UseControllerProps ,
5
5
useFormState ,
6
6
} from 'react-hook-form' ;
7
+ import { zodResolver } from '@hookform/resolvers/zod' ;
8
+ import * as z from 'zod' ;
9
+ import polyglotI18nProvider from 'ra-i18n-polyglot' ;
10
+ import englishMessages from 'ra-language-english' ;
7
11
8
12
import { CoreAdminContext } from '../core' ;
9
13
import { Form } from './Form' ;
10
14
import { useInput } from './useInput' ;
15
+ import { required } from './validate' ;
16
+ import ValidationError from './ValidationError' ;
17
+ import { mergeTranslations } from '../i18n' ;
18
+ import { I18nProvider } from '../types' ;
11
19
12
20
export default {
13
21
title : 'ra-core/form/Form' ,
@@ -32,7 +40,9 @@ const Input = props => {
32
40
aria-invalid = { fieldState . invalid }
33
41
{ ...field }
34
42
/>
35
- < p > { fieldState . error ?. message } </ p >
43
+ { fieldState . error && fieldState . error . message ? (
44
+ < ValidationError error = { fieldState . error . message } />
45
+ ) : null }
36
46
</ div >
37
47
) ;
38
48
} ;
@@ -164,3 +174,119 @@ export const UndefinedValue = () => {
164
174
</ CoreAdminContext >
165
175
) ;
166
176
} ;
177
+
178
+ const defaultI18nProvider = polyglotI18nProvider ( ( ) =>
179
+ mergeTranslations ( englishMessages , {
180
+ app : { validation : { required : 'This field must be provided' } } ,
181
+ } )
182
+ ) ;
183
+
184
+ export const FormLevelValidation = ( {
185
+ i18nProvider = defaultI18nProvider ,
186
+ } : {
187
+ i18nProvider ?: I18nProvider ;
188
+ } ) => {
189
+ const [ submittedData , setSubmittedData ] = React . useState < any > ( ) ;
190
+ return (
191
+ < CoreAdminContext i18nProvider = { i18nProvider } >
192
+ < Form
193
+ onSubmit = { data => setSubmittedData ( data ) }
194
+ record = { { id : 1 , field1 : 'bar' , field6 : null } }
195
+ validate = { ( values : any ) => {
196
+ const errors : any = { } ;
197
+ if ( ! values . defaultMessage ) {
198
+ errors . defaultMessage = 'ra.validation.required' ;
199
+ }
200
+ if ( ! values . customMessage ) {
201
+ errors . customMessage = 'This field is required' ;
202
+ }
203
+ if ( ! values . customMessageTranslationKey ) {
204
+ errors . customMessageTranslationKey =
205
+ 'app.validation.required' ;
206
+ }
207
+ if ( ! values . missingCustomMessageTranslationKey ) {
208
+ errors . missingCustomMessageTranslationKey =
209
+ 'app.validation.missing' ;
210
+ }
211
+ return errors ;
212
+ } }
213
+ >
214
+ < Input source = "defaultMessage" />
215
+ < Input source = "customMessage" />
216
+ < Input source = "customMessageTranslationKey" />
217
+ < Input source = "missingCustomMessageTranslationKey" />
218
+ < button type = "submit" > Submit</ button >
219
+ </ Form >
220
+ < pre > { JSON . stringify ( submittedData , null , 2 ) } </ pre >
221
+ </ CoreAdminContext >
222
+ ) ;
223
+ } ;
224
+
225
+ export const InputLevelValidation = ( {
226
+ i18nProvider = defaultI18nProvider ,
227
+ } : {
228
+ i18nProvider ?: I18nProvider ;
229
+ } ) => {
230
+ const [ submittedData , setSubmittedData ] = React . useState < any > ( ) ;
231
+ return (
232
+ < CoreAdminContext i18nProvider = { i18nProvider } >
233
+ < Form
234
+ onSubmit = { data => setSubmittedData ( data ) }
235
+ record = { { id : 1 , field1 : 'bar' , field6 : null } }
236
+ >
237
+ < Input source = "defaultMessage" validate = { required ( ) } />
238
+ < Input
239
+ source = "customMessage"
240
+ validate = { required ( 'This field is required' ) }
241
+ />
242
+ < Input
243
+ source = "customMessageTranslationKey"
244
+ validate = { required ( 'app.validation.required' ) }
245
+ />
246
+ < Input
247
+ source = "missingCustomMessageTranslationKey"
248
+ validate = { required ( 'app.validation.missing' ) }
249
+ />
250
+ < button type = "submit" > Submit</ button >
251
+ </ Form >
252
+ < pre > { JSON . stringify ( submittedData , null , 2 ) } </ pre >
253
+ </ CoreAdminContext >
254
+ ) ;
255
+ } ;
256
+
257
+ const zodSchema = z . object ( {
258
+ defaultMessage : z . string ( ) , //.min(1),
259
+ customMessage : z . string ( {
260
+ required_error : 'This field is required' ,
261
+ } ) ,
262
+ customMessageTranslationKey : z . string ( {
263
+ required_error : 'app.validation.required' ,
264
+ } ) ,
265
+ missingCustomMessageTranslationKey : z . string ( {
266
+ required_error : 'app.validation.missing' ,
267
+ } ) ,
268
+ } ) ;
269
+
270
+ export const ZodResolver = ( {
271
+ i18nProvider = defaultI18nProvider ,
272
+ } : {
273
+ i18nProvider ?: I18nProvider ;
274
+ } ) => {
275
+ const [ result , setResult ] = React . useState < any > ( ) ;
276
+ return (
277
+ < CoreAdminContext i18nProvider = { i18nProvider } >
278
+ < Form
279
+ record = { { } }
280
+ onSubmit = { data => setResult ( data ) }
281
+ resolver = { zodResolver ( zodSchema ) }
282
+ >
283
+ < Input source = "defaultMessage" />
284
+ < Input source = "customMessage" />
285
+ < Input source = "customMessageTranslationKey" />
286
+ < Input source = "missingCustomMessageTranslationKey" />
287
+ < button type = "submit" > Submit</ button >
288
+ </ Form >
289
+ < pre > { JSON . stringify ( result , null , 2 ) } </ pre >
290
+ </ CoreAdminContext >
291
+ ) ;
292
+ } ;
0 commit comments