Skip to content

Commit

Permalink
Merge pull request #61 from AkshataKatwal16/admin
Browse files Browse the repository at this point in the history
Issue #PS-1499 feat: Add custom validation for name and email field
  • Loading branch information
itsvick authored Jul 29, 2024
2 parents 99d11d6 + d319c98 commit 2408d9b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
6 changes: 4 additions & 2 deletions public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,10 @@
"MAX_LENGTH_DIGITS_ERROR": "Maximum {{maxLength}} Digits allowed",
"MIN_LENGTH_CHARACTERS_ERROR": "Minimum {{minLength}} characters required",
"MAX_LENGTH_CHARACTERS_ERROR": "Maximum {{maxLength}} characters allowed",
"NUMBER_AND_SPECIAL_CHARACTERS_NOT_ALLOWED": "Numbers and special characters are not allowed"
},
"NUMBER_AND_SPECIAL_CHARACTERS_NOT_ALLOWED": "Numbers and special characters are not allowed",
"NAME_CANNOT_INCLUDE_DIGITS":"Name can not include digits",
"INVALID_EMAIL_FORMAT":"Invalid email format"
},
"TABLE_TITLE": {
"NAME": "Name",
"STATUS": "Status",
Expand Down
12 changes: 3 additions & 9 deletions src/components/DynamicForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ interface DynamicFormProps {
};
children?: ReactNode;
}

const DynamicForm: React.FC<DynamicFormProps> = ({
schema,
uiSchema,
Expand All @@ -44,11 +45,8 @@ const DynamicForm: React.FC<DynamicFormProps> = ({
};
const { t } = useTranslation();

// console.log('CustomErrorList', CustomErrorList);

const handleError = (errors: any) => {
if (errors.length > 0) {
// Adjust the selector based on the actual structure of the form element names
const property = errors[0].property?.replace(/^root\./, '');
const errorField = document.querySelector(
`[name$="${property}"]`
Expand All @@ -57,7 +55,6 @@ const DynamicForm: React.FC<DynamicFormProps> = ({
if (errorField) {
errorField.focus();
} else {
// If the name-based selector fails, try to select by ID as a fallback
const fallbackField = document.getElementById(property) as HTMLElement;
if (fallbackField) {
fallbackField.focus();
Expand All @@ -73,16 +70,11 @@ const DynamicForm: React.FC<DynamicFormProps> = ({
return errors.map((error: any) => {
switch (error.name) {
case 'required': {
// error.message = t('FORM_ERROR_MESSAGES.FIELD_REQUIRED', {
// field: t(`FORM.${schema.properties[error.property].title}`),
// });

error.message = t('FORM_ERROR_MESSAGES.THIS_IS_REQUIRED_FIELD');
break;
}
case 'pattern': {
const property = error.property.substring(1);
console.log('schema===>', schema);
if (schema.properties?.[property]?.validation?.includes('numeric')) {
error.message = t('FORM_ERROR_MESSAGES.ENTER_ONLY_DIGITS');
} else if (
Expand Down Expand Up @@ -125,6 +117,7 @@ const DynamicForm: React.FC<DynamicFormProps> = ({
onChange(event);
}


return (
<div>
<FormWithMaterialUI
Expand All @@ -141,6 +134,7 @@ const DynamicForm: React.FC<DynamicFormProps> = ({
onError={handleError}
transformErrors={transformErrors}
fields={customFields}

>
{children}
</FormWithMaterialUI>
Expand Down
2 changes: 1 addition & 1 deletion src/components/UserTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ const UserTable: React.FC<UserTableProps> = ({ role , userType, searchPlaceholde
name: user.name.charAt(0).toUpperCase() + user.name.slice(1).toLowerCase(),
role: user.role,
gender:user.gender,
mobile: user.mobile==="NAN"?"":user.mobile,
mobile: user.mobile==="NaN"?"":user.mobile,
age: ageField ? ageField.value : null,
district: districtField ? districtField.value : null,
state: stateField ? stateField.value : null,
Expand Down

0 comments on commit 2408d9b

Please sign in to comment.