@@ -1215,12 +1215,17 @@ The value of the form `validate` prop must be a function taking the record as in
1215
1215
const validateUserCreation = (values ) => {
1216
1216
const errors = {};
1217
1217
if (! values .firstName ) {
1218
- errors .firstName = [ ' The firstName is required' ] ;
1218
+ errors .firstName = ' The firstName is required' ;
1219
1219
}
1220
1220
if (! values .age ) {
1221
- errors .age = [' The age is required' ];
1221
+ // You can return translation keys
1222
+ errors .age = ' ra.validation.required' ;
1222
1223
} else if (values .age < 18 ) {
1223
- errors .age = [' Must be over 18' ];
1224
+ // Or an object if the translation messages need parameters
1225
+ errors .age = {
1226
+ message: ' ra.validation.minValue' ,
1227
+ args: { min: 18 }
1228
+ };
1224
1229
}
1225
1230
return errors
1226
1231
};
@@ -1272,7 +1277,7 @@ const validateFirstName = [required(), minLength(2), maxLength(15)];
1272
1277
const validateEmail = email ();
1273
1278
const validateAge = [number (), minValue (18 )];
1274
1279
const validateZipCode = regex (/ ^ \d {5} $ / , ' Must be a valid Zip Code' );
1275
- const validateSex = choices ([' m' , ' f' ], ' Must be Male or Female ' );
1280
+ const validateGender = choices ([' m' , ' f' , ' nc ' ], ' Please choose one of the values ' );
1276
1281
1277
1282
export const UserCreate = (props ) => (
1278
1283
< Create {... props}>
@@ -1281,10 +1286,11 @@ export const UserCreate = (props) => (
1281
1286
< TextInput label= " Email" source= " email" validate= {validateEmail} / >
1282
1287
< TextInput label= " Age" source= " age" validate= {validateAge}/ >
1283
1288
< TextInput label= " Zip Code" source= " zip" validate= {validateZipCode}/ >
1284
- < SelectInput label= " Sex " source= " sex " choices= {[
1289
+ < SelectInput label= " Gender " source= " gender " choices= {[
1285
1290
{ id: ' m' , name: ' Male' },
1286
1291
{ id: ' f' , name: ' Female' },
1287
- ]} validate= {validateSex}/ >
1292
+ { id: ' nc' , name: ' Prefer not say' },
1293
+ ]} validate= {validateGender}/ >
1288
1294
< / SimpleForm>
1289
1295
< / Create>
1290
1296
);
@@ -1319,7 +1325,7 @@ const ageValidation = (value, allValues) => {
1319
1325
if (value < 18 ) {
1320
1326
return ' Must be over 18' ;
1321
1327
}
1322
- return [] ;
1328
+ return undefined ;
1323
1329
};
1324
1330
1325
1331
const validateFirstName = [required (), maxLength (15 )];
@@ -1414,17 +1420,25 @@ You can validate the entire form data server-side by returning a Promise in the
1414
1420
const validateUserCreation = async (values ) => {
1415
1421
const errors = {};
1416
1422
if (! values .firstName ) {
1417
- errors .firstName = [ ' The firstName is required' ] ;
1423
+ errors .firstName = ' The firstName is required' ;
1418
1424
}
1419
1425
if (! values .age ) {
1420
- errors .age = [ ' The age is required' ] ;
1426
+ errors .age = ' The age is required' ;
1421
1427
} else if (values .age < 18 ) {
1422
- errors .age = [ ' Must be over 18' ] ;
1428
+ errors .age = ' Must be over 18' ;
1423
1429
}
1424
1430
1425
- const isEmailUnique = await checkEmailIsUnique (values .userName );
1431
+ const isEmailUnique = await checkEmailIsUnique (values .email );
1426
1432
if (! isEmailUnique) {
1427
- errors .email = [' Email already used' ];
1433
+ // Return a message directly
1434
+ errors .email = ' Email already used' ;
1435
+ // Or a translation key
1436
+ errors .email = ' myapp.validation.email_not_unique' ;
1437
+ // Or an object if the translation needs parameters
1438
+ errors .email = {
1439
+ message: ' myapp.validation.email_not_unique' ,
1440
+ args: { email: values .email }
1441
+ };
1428
1442
}
1429
1443
return errors
1430
1444
};
@@ -1515,6 +1529,8 @@ export const UserCreate = (props) => {
1515
1529
1516
1530
** Tip** : The shape of the returned validation errors must correspond to the form: a key needs to match a ` source ` prop.
1517
1531
1532
+ ** Tip** : The returned validation errors might have any validation format we support (simple strings or object with message and args) for each key.
1533
+
1518
1534
## Submit On Enter
1519
1535
1520
1536
By default, pressing ` ENTER ` in any of the form fields submits the form - this is the expected behavior in most cases. However, some of your custom input components (e.g. Google Maps widget) may have special handlers for the ` ENTER ` key. In that case, to disable the automated form submission on enter, set the ` submitOnEnter ` prop of the form component to ` false ` :
0 commit comments