Skip to content

Commit

Permalink
fix: add validation to user registration form
Browse files Browse the repository at this point in the history
  • Loading branch information
dleard committed Jun 16, 2021
1 parent 9c687b8 commit a75b95a
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions app/containers/User/UserForm.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React, {useState} from 'react';
import {createFragmentContainer, graphql, RelayProp} from 'react-relay';
import JsonSchemaForm, {IChangeEvent} from '@rjsf/core';
import JsonSchemaForm, {IChangeEvent, AjvError} from '@rjsf/core';
import {JSONSchema7} from 'json-schema';
import {UserForm_user} from 'UserForm_user.graphql';
import createUserMutation from 'mutations/user/createUserMutation';
import updateUserMutation from 'mutations/user/updateUserMutation';
import FormArrayFieldTemplate from 'containers/Forms/FormArrayFieldTemplate';
import FormFieldTemplate from 'containers/Forms/FormFieldTemplate';
import FormObjectFieldTemplate from 'containers/Forms/FormObjectFieldTemplate';
import {customTransformErrors} from 'functions/customTransformErrors';

interface Props {
user?: UserForm_user;
Expand All @@ -19,6 +20,21 @@ interface Props {
onSubmit: () => any;
}

const customFormats = {
phoneNumber:
'^(\\+?\\d{1,2}[\\s,-]?)?\\(?\\d{3}\\)?[\\s.-]?\\d{3}[\\s.-]?\\d{4}$'
};

const customFormatsErrorMessages = {
phoneNumber:
'Please enter a valid phone number. eg: 1-234-567-8943, 123 456 7890, +12 345 678 9012',
email: 'Please enter a valid email address. eg: mail@example.com'
};

const transformErrors = (errors: AjvError[]) => {
return customTransformErrors(errors, customFormatsErrorMessages);
};

const UserForm: React.FunctionComponent<Props> = ({
user,
uuid,
Expand Down Expand Up @@ -82,11 +98,13 @@ const UserForm: React.FunctionComponent<Props> = ({
emailAddress: {
type: 'string',
title: 'Email Address',
default: defaultEmail
default: defaultEmail,
format: 'email'
},
phoneNumber: {
type: 'string',
title: 'Phone Number'
title: 'Phone Number',
format: 'phoneNumber'
},
occupation: {
type: 'string',
Expand All @@ -106,7 +124,10 @@ const UserForm: React.FunctionComponent<Props> = ({
<JsonSchemaForm
omitExtraData
liveOmit
liveValidate
schema={userSchema}
customFormats={customFormats}
transformErrors={transformErrors}
formData={user}
showErrorList={false}
ArrayFieldTemplate={FormArrayFieldTemplate}
Expand Down

0 comments on commit a75b95a

Please sign in to comment.