In this tutorial, I'll cover how to create a Material UI with React Hook Form - Validation with Error Messages. Scroll down to the end of the page to you can see the YouTube video(CodeAT21) and GitHub repository full source code available for this.
This react form is using the TextField, Radio, RadioGroup, FormControlLabel, FormControl, FormLabel, FormHelperText, FormGroup, Checkbox, and Button components from Material-UI. The elements we're adding are: five fields for the user to input their first name, last name, email, gender, I agree all terms and conditions and submit button.
Setup
First, we'll create the react project and install the two npm packages. In open your system terminal/command line enter the code below :
npx create-react-app form-validation
cd form-validation
npm install @mui/material @emotion/react @emotion/styled
npm install react-hook-form
npm start
Next, Project structure your folder as follows.
src
├── App.css
├── App.js
├── index.css
└── index.js
Next, open up the App.js file located in the src folder. You should see this:
App.jsimport React from 'react'; import TextField from '@mui/material/TextField'; import './App.css'; import { Button, Radio, RadioGroup, FormControlLabel, FormControl, FormLabel, FormHelperText, FormGroup, Checkbox, } from '@mui/material'; import { useForm } from "react-hook-form"; const App = () => { const { register, handleSubmit, watch, formState: { errors } } = useForm(); const onSubmit = (data) => console.log(data); console.log(errors); return ( <div className="App__form"> <h1> Material UI with React Hook Form - Validation with Error Messages </h1> <form onSubmit={handleSubmit(onSubmit)}> <TextField id="outlined-basic" name="firstName" label="First Name" variant="outlined" fullWidth {...register("firstName", { required: "First Name is required." })} error={Boolean(errors.firstName)} helperText={errors.firstName?.message} /> <TextField id="outlined-basic" label="Last Name" variant="outlined" fullWidth name="lastName" {...register("lastName", { required: "Last Name is required." })} error={Boolean(errors.lastName)} helperText={errors.lastName?.message} /> <TextField id="outlined-basic" label="E-mail" variant="outlined" fullWidth name="email" {...register("email", { required: "E-mail Address is required." })} error={Boolean(errors.email)} helperText={errors.email?.message} /> {/* Radio button */} <FormControl error={Boolean(errors.gender)} > <FormLabel component="legend"> Choose Your Gender </FormLabel> <RadioGroup row aria-label="gender" name="gender"> <FormControlLabel value="female" control={ <Radio {...register("gender", { required: "Choose your gender" })} /> } label="Female" /> <FormControlLabel value="male" control={ <Radio {...register("gender", { required: "Choose your gender" })} /> } label="Male" /> <FormControlLabel value="other" control={ <Radio {...register("gender", { required: "Choose your gender" })} /> } label="Other" /> </RadioGroup> <FormHelperText style={{color:'#d32f2f'}}>{errors.gender?.message}</FormHelperText> </FormControl> <div className="clearfix"></div> {/* Check box */} <FormGroup error={Boolean(errors.tnc)} style={{ display: "block", marginTop: "17px" }} > <FormControlLabel control={ <Checkbox name="tnc" {...register("tnc", { required: "please aggre our terms and condtions" })} /> } label="I aggree all terms and conditions" /> </FormGroup> <FormHelperText style={{color:'#d32f2f'}}>{errors.tnc?.message}</FormHelperText> <div className="clearfix"></div> <Button variant="contained" color="primary" type="submit" className="btns"> create new account </Button> </form> </div> ) } export default App