This repository contains an example of a custom form in React using react-hook-form
and zod
for data validation. The form includes fields for name, email, password, and password confirmation, all validated according to a schema defined with zod
.
-
React: JavaScript library for building user interfaces.
-
React Hook Form: Library for managing forms in React simply and flexibly.
-
Zod: Library for schema-based data validation.
-
TypeScript: A superset of JavaScript that adds static types to the language.
src/
│
├── components/
│ ├── CustomForm/
│ │ └── CustomForm.tsx # Main form component
│ └── CustomInput/
│ └── CustomInput.tsx # Reusable component for form fields
│
├── models/
│ └── index.ts # Type definitions and validation schema with Zod
│
├── App.tsx # Main application component
├── index.tsx # React entry point
└── App.css # General application styles
-
Clone the repository:
git clone https://github.com/your-username/repo-name.git cd repo-name
-
Install dependencies:
npm install
-
Run the application:
npm start
This project provides a solid base for creating forms in React with robust validation. The modular structure allows you to easily add new fields and validations.
You can modify the validation schema in src/models/index.ts
to suit the needs of your form.
import * as z from "zod";
export const schema = z.object({
name: z.string().min(1, "Name is required"),
email: z.string().email("Must be a valid email"),
password: z.string().min(8, "Password must be at least 8 characters long"),
confirmPassword: z.string().min(8, "You must confirm your password")
.refine((val, ctx) => val === ctx.parent.password, {
message: "Passwords must match",
}),
});
export type FormValues = z.infer<typeof schema>;
Contributions are welcome! If you have any improvements or find any bugs, feel free to open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for more details.