Skip to content

Commit

Permalink
Merge pull request #100 from AkshataKatwal16/admin
Browse files Browse the repository at this point in the history
Issue feat: Add required form validation changes
  • Loading branch information
itsvick authored Aug 5, 2024
2 parents 949c032 + 951290d commit 5b616f7
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 11 deletions.
3 changes: 2 additions & 1 deletion public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
"USERS_EMAIL": "Users Email",
"STATE_NAME_REQUIRED": "State Name is Required",
"DISTRICT_NAME_REQUIRED": "District Name is Required",
"SELECT_STATE": "Select State"
"SELECT_STATE": "Select State" ,
"SOMETHING_WENT_WRONG":"Something went wrong"
},
"LOGIN_PAGE": {
"USERNAME": "Username",
Expand Down
12 changes: 10 additions & 2 deletions src/components/CommonUserModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,15 @@ const CommonUserModal: React.FC<UserModalProps> = ({
fieldSchema?.hasOwnProperty("isDropdown") ||
fieldSchema?.hasOwnProperty("isCheckbox")
) {
apiBody.customFields.push({

apiBody.customFields.push({
fieldId: fieldId,
value: [String(fieldValue)],
});


} else {
if (fieldSchema.checkbox && fieldSchema.type === "array") {
if (fieldSchema.checkbox && fieldSchema.type === "array" && isEditModal) {
apiBody.customFields.push({
fieldId: fieldId,
value: String(fieldValue).split(","),
Expand Down Expand Up @@ -275,6 +278,8 @@ const CommonUserModal: React.FC<UserModalProps> = ({
showToastMessage(t(messageKey), "success");
} else {
const response = await createUser(apiBody);
if (response) {

const messageKey =
userType === FormContextType.STUDENT
? "LEARNERS.LEARNER_CREATED_SUCCESSFULLY"
Expand All @@ -283,10 +288,13 @@ const CommonUserModal: React.FC<UserModalProps> = ({
: "TEAM_LEADERS.TEAM_LEADER_CREATED_SUCCESSFULLY";

showToastMessage(t(messageKey), "success");
}
}
onSubmit(true);
onClose();
} catch (error) {
showToastMessage(t('COMMON.SOMETHING_WENT_WRONG'), 'error');

onClose();
console.log(error);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/DynamicForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const DynamicForm: React.FC<DynamicFormProps> = ({
const property = error.property.substring(1);

switch (pattern) {
case "^[a-z A-Z]+$": {
case '^[a-zA-Z][a-zA-Z ]*[a-zA-Z]$':{
error.message = t(
"FORM_ERROR_MESSAGES.NUMBER_AND_SPECIAL_CHARACTERS_NOT_ALLOWED",
);
Expand Down
9 changes: 7 additions & 2 deletions src/components/GeneratedSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { UiSchema } from "@rjsf/utils";
import { JSONSchema7 } from "json-schema";
import NumberInputField from "./form/NumberInputField";
import { FormData, Field, FieldOption } from "@/utils/Interfaces";
import { getCurrentYearPattern } from "@/utils/Helper";
export const customFields = {
NumberInputField: NumberInputField,
};
Expand Down Expand Up @@ -34,6 +35,7 @@ export const GenerateSchemaAndUiSchema = (
dependsOn,
pattern,
required,
isRequired
} = field;
const fieldSchema: any = {
title: t(`FORM.${label}`),
Expand Down Expand Up @@ -191,7 +193,7 @@ export const GenerateSchemaAndUiSchema = (
fieldSchema.pattern = pattern;
// fieldUiSchema["ui:help"]= "Only alphabetic characters are allowed.";
}
if (required) {
if (isRequired) {
schema.required?.push(name);
}
if (field?.minLength) {
Expand All @@ -201,9 +203,12 @@ export const GenerateSchemaAndUiSchema = (
fieldSchema.maxLength = Number(field.maxLength);
}
if (field?.validation) {
if (field?.validation?.includes("numeric")) {
if (field?.validation?.includes('numeric')) {
// fieldUiSchema['ui:field'] = 'NumberInputField';
}
if (field?.validation?.includes('currentYear')) {
fieldSchema.pattern = getCurrentYearPattern();
}
fieldSchema.validation = field.validation;
}
if (schema !== undefined && schema.properties) {
Expand Down
10 changes: 5 additions & 5 deletions src/data/tableColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ export const getUserTableColumns = (t: any, isMobile: any) => {
key: "name",
title: t("FORM.NAME"),
dataType: DataType.String,
sortDirection: SortDirection.Ascend,
// sortDirection: SortDirection.Ascend,
width: isMobile ? 160 : null,
},
{
key: "status",
title: t("FORM.STATUS"),
dataType: DataType.String,
sortDirection: SortDirection.Ascend,
/// sortDirection: SortDirection.Ascend,
width: isMobile ? 160 : null,
},

Expand Down Expand Up @@ -79,14 +79,14 @@ export const getTLTableColumns = (t: any, isMobile: any) => {
key: "name",
title: t("FORM.NAME"),
dataType: DataType.String,
sortDirection: SortDirection.Ascend,
//sortDirection: SortDirection.Ascend,
// width: isMobile?160:null,
},
{
key: "status",
title: t("FORM.STATUS"),
dataType: DataType.String,
sortDirection: SortDirection.Ascend,
// sortDirection: SortDirection.Ascend,
// width: isMobile?160:null,
},

Expand Down Expand Up @@ -122,7 +122,7 @@ export const getTLTableColumns = (t: any, isMobile: any) => {
key: "blocks",
title: t("FORM.BLOCK"),
dataType: DataType.String,
sortDirection: SortDirection.Ascend,
// sortDirection: SortDirection.Ascend,
// width: isMobile?160:null,
},

Expand Down
4 changes: 4 additions & 0 deletions src/utils/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,7 @@ export const fieldTextValidation = (text: string) => {
const regex = /^[A-Za-z\s]+$/;
return regex.test(text);
};
export const getCurrentYearPattern = () => {
const currentYear = new Date().getFullYear();
return `^(19[0-9][0-9]|20[0-${Math.floor(currentYear / 10) % 10}][0-${currentYear % 10}])$`;
};
4 changes: 4 additions & 0 deletions src/utils/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export interface Field {
fieldId: string;
required?: boolean;
default: string | number;
isRequired?: boolean;

}
export interface TenantCohortRoleMapping {
tenantId: string;
Expand All @@ -39,6 +41,8 @@ export interface TenantCohortRoleMapping {
export interface CustomField {
fieldId: string;
value: string;
isRequired?: boolean;

}
export interface FormData {
formid: string;
Expand Down

0 comments on commit 5b616f7

Please sign in to comment.