From 2401d3eea2058a719bb44fa1c69b932414d93d58 Mon Sep 17 00:00:00 2001 From: sonika <> Date: Sun, 20 Aug 2023 19:53:11 +0530 Subject: [PATCH] second form validation --- src/Components/PatientForm/FormSection2.tsx | 206 +++++++++++++++++--- 1 file changed, 174 insertions(+), 32 deletions(-) diff --git a/src/Components/PatientForm/FormSection2.tsx b/src/Components/PatientForm/FormSection2.tsx index 09ebde0..b7069f3 100644 --- a/src/Components/PatientForm/FormSection2.tsx +++ b/src/Components/PatientForm/FormSection2.tsx @@ -1,36 +1,75 @@ -import * as React from 'react'; import { Button, Checkbox, FormControl, FormControlLabel, FormGroup, FormLabel, Grid, Paper, Radio, RadioGroup, TextField } from '@mui/material'; import { useFormik } from "formik"; import * as yup from "yup"; import { LocalizationProvider, DatePicker } from '@mui/x-date-pickers'; -export default function FormSection1(){ - const [state, setState] = React.useState({ - married: false, - single: false, - widowed: false, - separated: false, - divorced: false, - }); - - const handleChange = (event: React.ChangeEvent) => { - setState({ ...state, [event.target.name]: event.target.checked }); - }; +interface FormValues { + maritalStatus: string; + numberOfChildren: string; + ages: string; + occupation: string; + hoursPerWeek: number | string; + employer: string; + businessPhone: string; + spouseName: string; + spouseEmployer: string; + spouseBusinessPhone: string; + emergencyContact: string; + relationship: string; + phone: string; +} - +const validationSchema = yup.object({ + maritalStatus:yup.string().required("Marital Status is required"), + numberOfChildren:yup.string().required("Full name is required"), + ages:yup.string().required("Full name is required"), + occupation:yup.string().required("Occupation is required"), +// hoursPerWeek: yup.number().required('Required'), +// employer:yup.string().required("Full name is required"), +// businessPhone:yup.string().required("Full name is required"), + spouseName:yup.string().required("Spouse name is required"), +// spouseEmployer:yup.string().required("Full name is required"), +// spouseBusinessPhone:yup.string().required("Full name is required"), + emergencyContact:yup.string().matches(/^\d{10}$/, "Cell phone must be 10 digits"), + relationship:yup.string().required("Relationship is required"), + phone:yup.string().matches(/^\d{10}$/, "Cell phone must be 10 digits"), +}); + +const Form: React.FC = () => { + const formik = useFormik({ + initialValues:{ + maritalStatus:'', + numberOfChildren:'', + ages:'', + occupation:'', + hoursPerWeek:'', + employer:'', + businessPhone:'', + spouseName:'', + spouseEmployer:'', + spouseBusinessPhone:'', + emergencyContact:'', + relationship:'', + phone:'' + }, + validationSchema, + onSubmit:(values)=>{ + console.log(values); + } + }) return( <> - -
+ - + Marital Status } label="Married" /> @@ -39,45 +78,148 @@ export default function FormSection1(){ } label="Seperated" /> } label="Divorced" /> - + {formik.touched.maritalStatus && formik.errors.maritalStatus ? ( +
{formik.errors.maritalStatus}
+ ) : null} +
- + - + + - + + - + - + - + - + - + - + - + - +
+ ) -} \ No newline at end of file +}; + +export default Form; \ No newline at end of file