From b38bab5ddd2a41b76ffa927f807484e09e6a51a3 Mon Sep 17 00:00:00 2001 From: IshaVenikar Date: Thu, 17 Oct 2024 18:22:48 +0530 Subject: [PATCH] Add validation for form fields --- .../components/projects/create/Configure.tsx | 3 ++ .../projects/create/template/index.tsx | 3 +- .../id/settings/EnvironmentVariablesForm.tsx | 43 +++++++++---------- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/packages/frontend/src/components/projects/create/Configure.tsx b/packages/frontend/src/components/projects/create/Configure.tsx index 4f622326..75061055 100644 --- a/packages/frontend/src/components/projects/create/Configure.tsx +++ b/packages/frontend/src/components/projects/create/Configure.tsx @@ -233,6 +233,7 @@ const Configure = () => { ( )} @@ -252,6 +253,7 @@ const Configure = () => { ( )} @@ -264,6 +266,7 @@ const Configure = () => { ( )} diff --git a/packages/frontend/src/pages/org-slug/projects/create/template/index.tsx b/packages/frontend/src/pages/org-slug/projects/create/template/index.tsx index ebb9f3b9..4024e521 100644 --- a/packages/frontend/src/pages/org-slug/projects/create/template/index.tsx +++ b/packages/frontend/src/pages/org-slug/projects/create/template/index.tsx @@ -163,6 +163,7 @@ const CreateRepo = () => { ( )} @@ -172,7 +173,7 @@ const CreateRepo = () => { ( + render={({ }) => ( )} /> diff --git a/packages/frontend/src/pages/org-slug/projects/id/settings/EnvironmentVariablesForm.tsx b/packages/frontend/src/pages/org-slug/projects/id/settings/EnvironmentVariablesForm.tsx index c5860522..6b14b181 100644 --- a/packages/frontend/src/pages/org-slug/projects/id/settings/EnvironmentVariablesForm.tsx +++ b/packages/frontend/src/pages/org-slug/projects/id/settings/EnvironmentVariablesForm.tsx @@ -1,10 +1,10 @@ -import { useEffect } from 'react'; +import { useEffect, useMemo } from 'react'; import { useFieldArray, useFormContext } from 'react-hook-form'; import { Checkbox } from '@snowballtools/material-tailwind-react-fork'; import { Button } from 'components/shared/Button'; -// import { InlineNotification } from 'components/shared/InlineNotification'; +import { InlineNotification } from 'components/shared/InlineNotification'; import AddEnvironmentVariableRow from 'components/projects/project/settings/AddEnvironmentVariableRow'; import { EnvironmentVariablesFormValues } from 'types/types'; @@ -13,14 +13,11 @@ const EnvironmentVariablesForm = () => { register, control, reset, - formState: { isSubmitSuccessful }, + formState: { isSubmitSuccessful, errors }, } = useFormContext(); const { fields, append, remove } = useFieldArray({ name: 'variables', control, - rules: { - required: 'Add at least 1 environment variables', - }, }); useEffect(() => { @@ -29,20 +26,20 @@ const EnvironmentVariablesForm = () => { } }, [isSubmitSuccessful, reset]); - // const isFieldEmpty = useMemo(() => { - // if (errors.variables) { - // return fields.some((_, index) => { - // if ( - // errors.variables![index]?.value?.type === 'required' || - // errors.variables![index]?.key?.type === 'required' - // ) { - // return true; - // } - // }); - // } + const isFieldEmpty = useMemo(() => { + if (errors.variables) { + return fields.some((_, index) => { + if ( + errors.variables![index]?.value?.type === 'required' || + errors.variables![index]?.key?.type === 'required' + ) { + return true; + } + }); + } - // return false; - // }, [fields, errors.variables]); + return false; + }, [fields, errors.variables]); return ( <> @@ -52,7 +49,7 @@ const EnvironmentVariablesForm = () => { index={index} register={register} onDelete={() => remove(index)} - isDeleteDisabled={fields.length === 1} + isDeleteDisabled={fields.length === 0} /> ))}
@@ -60,18 +57,18 @@ const EnvironmentVariablesForm = () => { + Add variable
- {/* {isFieldEmpty && ( + {isFieldEmpty && ( - )} */} + )}
- + ); };