-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhook-forms.js
52 lines (42 loc) · 1.32 KB
/
hook-forms.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import React from "react";
import { useForm } from "react-hook-form";
/* Use Joi object validators */
import Joi from "joi";
/* Use Joi resolver for react hook form */
import { joiResolver } from "@hookform/resolvers/joi";
/* Persian Joi validators */
import joiExtenstions from "joi-persian-validators";
const pJoi = Joi.extend(...joiExtenstions);
const schema = pJoi.object({
mobile: pJoi.mobile().with({ operator: true }).messages({
'mobile.base': 'شماره موبایل وارد شده اشتباه است',
'mobile.characters': 'شماره موبایل باید دقیقا ۱۱ شماره باشد'
}),
id: pJoi.idNumber()
});
export default function App() {
const {
register,
handleSubmit,
formState: { errors }
} = useForm({
resolver: joiResolver(schema)
});
const onSubmit = (data) => {
console.log("data", data);
};
return (
<div className="App">
<h1>Persian Joi Validator</h1>
<form onSubmit={handleSubmit(onSubmit)}>
<label>شماره موبایل</label>
<input {...register("mobile")} />
{<div className="error">{errors.mobile?.message}</div>}
<label>کد ملی</label>
<input {...register("id")} />
{<div className="error">{errors.id?.message}</div>}
<input type="submit" />
</form>
</div>
);
}