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

View File

@ -163,6 +163,7 @@ const CreateRepo = () => {
<Controller <Controller
name="repoName" name="repoName"
control={control} control={control}
rules={{ required: true }}
render={({ field: { value, onChange } }) => ( render={({ field: { value, onChange } }) => (
<Input value={value} onChange={onChange} /> <Input value={value} onChange={onChange} />
)} )}
@ -172,7 +173,7 @@ const CreateRepo = () => {
<Controller <Controller
name="isPrivate" name="isPrivate"
control={control} control={control}
render={({}) => ( render={({ }) => (
<Checkbox label="Make this repo private" disabled={true} /> <Checkbox label="Make this repo private" disabled={true} />
)} )}
/> />

View File

@ -1,10 +1,10 @@
import { useEffect } from 'react'; import { useEffect, useMemo } from 'react';
import { useFieldArray, useFormContext } from 'react-hook-form'; import { useFieldArray, useFormContext } from 'react-hook-form';
import { Checkbox } from '@snowballtools/material-tailwind-react-fork'; import { Checkbox } from '@snowballtools/material-tailwind-react-fork';
import { Button } from 'components/shared/Button'; 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 AddEnvironmentVariableRow from 'components/projects/project/settings/AddEnvironmentVariableRow';
import { EnvironmentVariablesFormValues } from 'types/types'; import { EnvironmentVariablesFormValues } from 'types/types';
@ -13,14 +13,11 @@ const EnvironmentVariablesForm = () => {
register, register,
control, control,
reset, reset,
formState: { isSubmitSuccessful }, formState: { isSubmitSuccessful, errors },
} = useFormContext<EnvironmentVariablesFormValues>(); } = useFormContext<EnvironmentVariablesFormValues>();
const { fields, append, remove } = useFieldArray({ const { fields, append, remove } = useFieldArray({
name: 'variables', name: 'variables',
control, control,
rules: {
required: 'Add at least 1 environment variables',
},
}); });
useEffect(() => { useEffect(() => {
@ -29,20 +26,20 @@ const EnvironmentVariablesForm = () => {
} }
}, [isSubmitSuccessful, reset]); }, [isSubmitSuccessful, reset]);
// const isFieldEmpty = useMemo(() => { const isFieldEmpty = useMemo(() => {
// if (errors.variables) { if (errors.variables) {
// return fields.some((_, index) => { return fields.some((_, index) => {
// if ( if (
// errors.variables![index]?.value?.type === 'required' || errors.variables![index]?.value?.type === 'required' ||
// errors.variables![index]?.key?.type === 'required' errors.variables![index]?.key?.type === 'required'
// ) { ) {
// return true; return true;
// } }
// }); });
// } }
// return false; return false;
// }, [fields, errors.variables]); }, [fields, errors.variables]);
return ( return (
<> <>
@ -52,7 +49,7 @@ const EnvironmentVariablesForm = () => {
index={index} index={index}
register={register} register={register}
onDelete={() => remove(index)} onDelete={() => remove(index)}
isDeleteDisabled={fields.length === 1} isDeleteDisabled={fields.length === 0}
/> />
))} ))}
<div className="flex gap-1 p-2"> <div className="flex gap-1 p-2">
@ -60,12 +57,12 @@ const EnvironmentVariablesForm = () => {
+ Add variable + Add variable
</Button> </Button>
</div> </div>
{/* {isFieldEmpty && ( {isFieldEmpty && (
<InlineNotification <InlineNotification
title="Please ensure no fields are empty before saving." title="Please ensure no fields are empty before saving."
variant="danger" variant="danger"
/> />
)} */} )}
<div className="flex gap-2 p-2"> <div className="flex gap-2 p-2">
<Checkbox label="Production" {...register('environment.production')} /> <Checkbox label="Production" {...register('environment.production')} />
<Checkbox label="Preview" {...register('environment.preview')} /> <Checkbox label="Preview" {...register('environment.preview')} />