From ba6860cecfdd44911337afc63d3bf8a005a492a1 Mon Sep 17 00:00:00 2001 From: amitbadala Date: Thu, 2 Nov 2023 13:05:37 +0530 Subject: [PATCH 1/7] Add nonOptionalErrorMsg property --- examples/for-tests/src/App.js | 1 + lib/build/emailpassword-shared4.js | 3 +++ lib/build/types.d.ts | 2 ++ lib/ts/recipe/emailpassword/utils.ts | 3 +++ lib/ts/types.ts | 10 ++++++++++ 5 files changed, 19 insertions(+) diff --git a/examples/for-tests/src/App.js b/examples/for-tests/src/App.js index 505beeb61..c4a187964 100644 --- a/examples/for-tests/src/App.js +++ b/examples/for-tests/src/App.js @@ -301,6 +301,7 @@ const customFields = [ id: "terms", label: "", optional: false, + nonOptionalErrorMsg: "Terms and conditions is required", inputComponent: ({ name, onChange }) => (
Promise; optional?: boolean; + nonOptionalErrorMsg?: string; }; export declare type APIFormField = { id: string; @@ -105,6 +106,7 @@ export declare type NormalisedFormField = { placeholder: string; validate: (value: any) => Promise | string | undefined; optional: boolean; + nonOptionalErrorMsg?: string; autoComplete?: string; autofocus?: boolean; getDefaultValue?: () => string; diff --git a/lib/ts/recipe/emailpassword/utils.ts b/lib/ts/recipe/emailpassword/utils.ts index 6220af0c5..8edc3b0e5 100644 --- a/lib/ts/recipe/emailpassword/utils.ts +++ b/lib/ts/recipe/emailpassword/utils.ts @@ -343,6 +343,9 @@ export function getFormattedFormField(field: NormalisedFormField): NormalisedFor validate: async (value: any): Promise => { // Absent or not optional empty field if (value === "" && field.optional === false) { + if (field.nonOptionalErrorMsg !== undefined && field.nonOptionalErrorMsg !== "") { + return field.nonOptionalErrorMsg; + } return "ERROR_NON_OPTIONAL"; } diff --git a/lib/ts/types.ts b/lib/ts/types.ts index 16beba60b..3ac8ddcc8 100644 --- a/lib/ts/types.ts +++ b/lib/ts/types.ts @@ -239,6 +239,11 @@ export type FormField = FormFieldBaseConfig & { * Whether the field is optional or not. */ optional?: boolean; + + /* + * Error message for non optional field. + */ + nonOptionalErrorMsg?: string; }; export type APIFormField = { @@ -279,6 +284,11 @@ export type NormalisedFormField = { */ optional: boolean; + /* + * Error message for non optional field. + */ + nonOptionalErrorMsg?: string; + /* * Autocomplete input. */ From f0e5affb825a464ab24f8d92aa03365fb79bdd46 Mon Sep 17 00:00:00 2001 From: amitbadala Date: Fri, 3 Nov 2023 11:17:14 +0530 Subject: [PATCH 2/7] Overwrite be error message for non-optional field if nonOptionalErrorMsg is provided --- lib/build/emailpassword-shared7.js | 50 +++++++++++++++---- .../components/library/formBase.tsx | 19 +++++-- 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/lib/build/emailpassword-shared7.js b/lib/build/emailpassword-shared7.js index 0abdcd070..dc582c008 100644 --- a/lib/build/emailpassword-shared7.js +++ b/lib/build/emailpassword-shared7.js @@ -616,7 +616,17 @@ var FormBase = function (props) { var onFormSubmit = React.useCallback( function (e) { return genericComponentOverrideContext.__awaiter(void 0, void 0, void 0, function () { - var apiFields, fieldUpdates, result, generalError, e_1, _loop_1, _i, formFields_1, field, errorFields_1; + var apiFields, + fieldUpdates, + result, + generalError, + e_1, + _loop_1, + _i, + formFields_1, + field, + errorFields_1, + getErrorMessage_1; return genericComponentOverrideContext.__generator(this, function (_a) { switch (_a.label) { case 0: @@ -706,19 +716,39 @@ var FormBase = function (props) { // If field error. if (result.status === "FIELD_ERROR") { errorFields_1 = result.formFields; + getErrorMessage_1 = function (fs) { + var _a; + var errorMessage = + (_a = errorFields_1.find(function (ef) { + return ef.id === fs.id; + })) === null || _a === void 0 + ? void 0 + : _a.error; + if (errorMessage === "Field is not optional") { + var fieldConfigData = props.formFields.find(function (f) { + return f.id === fs.id; + }); + // set non-optional error message from nonOptionalErrorMsg + if ( + (fieldConfigData === null || fieldConfigData === void 0 + ? void 0 + : fieldConfigData.nonOptionalErrorMsg) !== undefined && + (fieldConfigData === null || fieldConfigData === void 0 + ? void 0 + : fieldConfigData.nonOptionalErrorMsg) !== "" + ) { + return fieldConfigData === null || fieldConfigData === void 0 + ? void 0 + : fieldConfigData.nonOptionalErrorMsg; + } + } + return errorMessage; + }; setFieldStates(function (os) { return os.map(function (fs) { - var _a; return genericComponentOverrideContext.__assign( genericComponentOverrideContext.__assign({}, fs), - { - error: - (_a = errorFields_1.find(function (ef) { - return ef.id === fs.id; - })) === null || _a === void 0 - ? void 0 - : _a.error, - } + { error: getErrorMessage_1(fs) } ); }); }); diff --git a/lib/ts/recipe/emailpassword/components/library/formBase.tsx b/lib/ts/recipe/emailpassword/components/library/formBase.tsx index f9d5e201b..b811c1bb2 100644 --- a/lib/ts/recipe/emailpassword/components/library/formBase.tsx +++ b/lib/ts/recipe/emailpassword/components/library/formBase.tsx @@ -251,10 +251,21 @@ export const FormBase: React.FC> = (props) => { // If field error. if (result.status === "FIELD_ERROR") { const errorFields = result.formFields; - - setFieldStates((os) => - os.map((fs) => ({ ...fs, error: errorFields.find((ef: any) => ef.id === fs.id)?.error })) - ); + const getErrorMessage = (fs: FieldState) => { + const errorMessage = errorFields.find((ef: any) => ef.id === fs.id)?.error; + if (errorMessage === "Field is not optional") { + const fieldConfigData = props.formFields.find((f) => f.id === fs.id); + // set non-optional error message from nonOptionalErrorMsg + if ( + fieldConfigData?.nonOptionalErrorMsg !== undefined && + fieldConfigData?.nonOptionalErrorMsg !== "" + ) { + return fieldConfigData?.nonOptionalErrorMsg; + } + } + return errorMessage; + }; + setFieldStates((os) => os.map((fs) => ({ ...fs, error: getErrorMessage(fs) }))); } } } catch (e) { From 3434cddcfd898a939b90b2ef3933bf2da83c7f40 Mon Sep 17 00:00:00 2001 From: amitbadala Date: Fri, 3 Nov 2023 12:20:22 +0530 Subject: [PATCH 3/7] Add nonOptionalErrorMsg for signup form --- examples/for-tests/src/App.js | 3 +- lib/build/emailpassword-shared7.js | 2 +- .../components/library/formBase.tsx | 2 +- test/end-to-end/signup.test.js | 75 +++++++++++++++++++ 4 files changed, 79 insertions(+), 3 deletions(-) diff --git a/examples/for-tests/src/App.js b/examples/for-tests/src/App.js index c4a187964..8c940e174 100644 --- a/examples/for-tests/src/App.js +++ b/examples/for-tests/src/App.js @@ -285,6 +285,7 @@ const customFields = [ { id: "select-dropdown", label: "Select Dropdown", + nonOptionalErrorMsg: "Select dropdown is not an optional", inputComponent: ({ value, name, onChange }) => (