Add validation for form fields

This commit is contained in:
IshaVenikar 2024-10-17 18:22:48 +05:30
parent 5b55e7783e
commit b38bab5ddd
3 changed files with 25 additions and 24 deletions

View File

@ -233,6 +233,7 @@ const Configure = () => {
<Controller
name="lrn"
control={methods.control}
rules={{ required : true }}
render={({ field: { value, onChange } }) => (
<Input value={value} onChange={onChange} />
)}
@ -252,6 +253,7 @@ const Configure = () => {
<Controller
name="numProviders"
control={methods.control}
rules={{ required : true }}
render={({ field: { value, onChange } }) => (
<Input type="number" value={value} onChange={onChange} />
)}
@ -264,6 +266,7 @@ const Configure = () => {
<Controller
name="maxPrice"
control={methods.control}
rules={{ required : true }}
render={({ field: { value, onChange } }) => (
<Input type="number" value={value} onChange={onChange} />
)}

View File

@ -163,6 +163,7 @@ const CreateRepo = () => {
<Controller
name="repoName"
control={control}
rules={{ required: true }}
render={({ field: { value, onChange } }) => (
<Input value={value} onChange={onChange} />
)}

View File

@ -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<EnvironmentVariablesFormValues>();
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}
/>
))}
<div className="flex gap-1 p-2">
@ -60,12 +57,12 @@ const EnvironmentVariablesForm = () => {
+ Add variable
</Button>
</div>
{/* {isFieldEmpty && (
{isFieldEmpty && (
<InlineNotification
title="Please ensure no fields are empty before saving."
variant="danger"
/>
)} */}
)}
<div className="flex gap-2 p-2">
<Checkbox label="Production" {...register('environment.production')} />
<Checkbox label="Preview" {...register('environment.preview')} />