From 83abd2a450204a4c13af0257a130a5ecff96f12c Mon Sep 17 00:00:00 2001 From: Gildas Garcia <1122076+djhi@users.noreply.github.com> Date: Fri, 9 Apr 2021 17:20:53 +0200 Subject: [PATCH 1/3] Fix Validation Documentation --- docs/CreateEdit.md | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/docs/CreateEdit.md b/docs/CreateEdit.md index e569fca10b7..75639b5b62c 100644 --- a/docs/CreateEdit.md +++ b/docs/CreateEdit.md @@ -1215,12 +1215,17 @@ The value of the form `validate` prop must be a function taking the record as in const validateUserCreation = (values) => { const errors = {}; if (!values.firstName) { - errors.firstName = ['The firstName is required']; + errors.firstName = 'The firstName is required'; } if (!values.age) { - errors.age = ['The age is required']; + // You can return translation keys + errors.age = 'ra.validation.required'; } else if (values.age < 18) { - errors.age = ['Must be over 18']; + // Or an object if the translation messages need parameters + errors.age = { + message: 'ra.validation.minValue', + args: { min: 18 } + }; } return errors }; @@ -1272,7 +1277,7 @@ const validateFirstName = [required(), minLength(2), maxLength(15)]; const validateEmail = email(); const validateAge = [number(), minValue(18)]; const validateZipCode = regex(/^\d{5}$/, 'Must be a valid Zip Code'); -const validateSex = choices(['m', 'f'], 'Must be Male or Female'); +const validateGenre = choices(['m', 'f', 'nc'], 'Please choose one of the values'); export const UserCreate = (props) => ( @@ -1281,10 +1286,11 @@ export const UserCreate = (props) => ( - + { id: 'nc', name: 'Prefer not say' }, + ]} validate={validateGenre}/> ); @@ -1319,7 +1325,7 @@ const ageValidation = (value, allValues) => { if (value < 18) { return 'Must be over 18'; } - return []; + return undefined; }; const validateFirstName = [required(), maxLength(15)]; @@ -1414,17 +1420,25 @@ You can validate the entire form data server-side by returning a Promise in the const validateUserCreation = async (values) => { const errors = {}; if (!values.firstName) { - errors.firstName = ['The firstName is required']; + errors.firstName = 'The firstName is required'; } if (!values.age) { - errors.age = ['The age is required']; + errors.age = 'The age is required'; } else if (values.age < 18) { - errors.age = ['Must be over 18']; + errors.age = 'Must be over 18'; } - const isEmailUnique = await checkEmailIsUnique(values.userName); + const isEmailUnique = await checkEmailIsUnique(values.email); if (!isEmailUnique) { - errors.email = ['Email already used']; + // Return a message directly + errors.email = 'Email already used'; + // Or a translation key + errors.email = 'myapp.validation.email_not_unique'; + // Or an object if the translation needs parameters + errors.email = { + message: 'myapp.validation.email_not_unique', + args: { email: values.email } + }; } return errors }; @@ -1515,6 +1529,8 @@ export const UserCreate = (props) => { **Tip**: The shape of the returned validation errors must correspond to the form: a key needs to match a `source` prop. +**Tip**: The returned validation errors might have any validation format we support (simple strings or object with message and args) for each key. + ## Submit On Enter 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`: From 1368e354d530d06d4ce66734de4b433be29642a9 Mon Sep 17 00:00:00 2001 From: Gildas Garcia <1122076+djhi@users.noreply.github.com> Date: Fri, 9 Apr 2021 17:21:22 +0200 Subject: [PATCH 2/3] Fix Submission Errors Cannot Have Translatable Error Objects --- examples/simple/src/users/UserEdit.tsx | 40 +++- packages/ra-core/package.json | 1 - .../ra-core/src/form/FormWithRedirect.tsx | 2 +- packages/ra-core/src/form/ValidationError.tsx | 1 - .../src/form/submitErrorsMutators.spec.tsx | 174 ++++++++++++++++++ .../ra-core/src/form/submitErrorsMutators.ts | 128 +++++++++++++ packages/react-admin/package.json | 1 - yarn.lock | 5 - 8 files changed, 334 insertions(+), 18 deletions(-) create mode 100644 packages/ra-core/src/form/submitErrorsMutators.spec.tsx create mode 100644 packages/ra-core/src/form/submitErrorsMutators.ts diff --git a/examples/simple/src/users/UserEdit.tsx b/examples/simple/src/users/UserEdit.tsx index 56c23578f74..b62afc9c66b 100644 --- a/examples/simple/src/users/UserEdit.tsx +++ b/examples/simple/src/users/UserEdit.tsx @@ -53,16 +53,26 @@ const EditActions = ({ basePath, data, hasShow }) => ( ); -const UserEdit = ({ permissions, ...props }) => ( - } - aside={