MCPcopy Create free account
hub / github.com/coderarmy-notes/mern-stack-course / Form

Function Form

04React/Day09/src/Form.jsx:3–62  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1import { useForm } from "react-hook-form"
2
3function Form(){
4
5 const {register, handleSubmit, formState: { errors }} = useForm();
6
7
8 function sumbitForm(data){
9 console.log(data);
10 }
11
12 console.log("Render");
13
14 return(
15 <>
16 <form onSubmit={handleSubmit(sumbitForm)}>
17 <div>
18 <label htmlFor="first">Name: </label>
19 <input id="first" {...register('name',
20 { required: "Name can't be empty"}
21 )} />
22 {errors.name && <span>{errors.name.message}</span>}
23 </div>
24 <div>
25 <label htmlFor="second">Age: </label>
26 <input id="second" {...register('age',
27 {
28 min:{
29 value:10,
30 message:"Minimum Age should be 10"
31 },
32 max:{
33 value:80,
34 message:"Maximum Age should be 80"
35 }
36 }
37 )} />
38 {errors.age && <span>{errors.age.message}</span>}
39 </div>
40 <div>
41 <label htmlFor="third">Password: </label>
42 <input type="password" id="third" {...register('password',
43 {
44 minLength:{
45 value:5,
46 message:"Minimum Length of password should be 5"
47 },
48 maxLength:{
49 value:20,
50 message:"Maximum length of password should be 20"
51 }
52 }
53 )} />
54 {errors.password && <span>{errors.password.message}</span>}
55 </div>
56 <button>Submit</button>
57 </form>
58
59 </>
60 )

Callers

nothing calls this directly

Calls 1

handleSubmitFunction · 0.70

Tested by

no test coverage detected