File size: 3,621 Bytes
1e92f2d |
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
import React from 'react';
import { useForm, ValidationMode } from 'react-hook-form';
import Joi from 'joi';
import { useParams } from 'react-router-dom';
let renderCounter = 0;
const validationSchema = Joi.object({
firstName: Joi.string().required(),
lastName: Joi.string().max(5).required(),
min: Joi.number().min(10).required(),
max: Joi.number().max(20).required(),
minDate: Joi.date().min('2019-08-01'),
maxDate: Joi.date().max('2019-08-01'),
minLength: Joi.string().min(2),
minRequiredLength: Joi.string().required(),
selectNumber: Joi.string().required(),
pattern: Joi.string().required(),
radio: Joi.string().required(),
checkbox: Joi.required(),
});
const resolver = async (data: unknown) => {
const { error, value: values } = validationSchema.validate(data, {
abortEarly: false,
});
return {
values: error ? {} : values,
errors: error
? error.details.reduce((previous, { message, type, path }) => {
return {
...previous,
[path[0]]: {
message,
type,
},
};
}, {})
: {},
};
};
const BasicSchemaValidation: React.FC = () => {
const { mode } = useParams();
const {
register,
handleSubmit,
formState: { errors },
} = useForm<{
firstName: string;
lastName: string;
min: string;
max: string;
minDate: string;
maxDate: string;
minLength: string;
minRequiredLength: string;
selectNumber: string;
pattern: string;
radio: string;
checkbox: string;
multiple: string;
validate: string;
}>({
resolver,
mode: mode as keyof ValidationMode,
});
const onSubmit = () => {};
renderCounter++;
return (
<form onSubmit={handleSubmit(onSubmit)}>
<input {...register('firstName')} placeholder="firstName" />
{errors.firstName && <p>firstName error</p>}
<input {...register('lastName')} placeholder="lastName" />
{errors.lastName && <p>lastName error</p>}
<input type="number" {...register('min')} placeholder="min" />
{errors.min && <p>min error</p>}
<input type="number" {...register('max')} placeholder="max" />
{errors.max && <p>max error</p>}
<input type="date" {...register('minDate')} placeholder="minDate" />
{errors.minDate && <p>minDate error</p>}
<input type="date" {...register('maxDate')} placeholder="maxDate" />
{errors.maxDate && <p>maxDate error</p>}
<input {...register('minLength')} placeholder="minLength" />
{errors.minLength && <p>minLength error</p>}
<input
{...register('minRequiredLength')}
placeholder="minRequiredLength"
/>
{errors.minRequiredLength && <p>minRequiredLength error</p>}
<select {...register('selectNumber')}>
<option value="">Select</option>
<option value={1}>1</option>
<option value={2}>1</option>
</select>
{errors.selectNumber && <p>selectNumber error</p>}
<input {...register('pattern')} placeholder="pattern" />
{errors.pattern && <p>pattern error</p>}
Radio1
<input type="radio" {...register('radio')} value="1" />
Radio2
<input type="radio" {...register('radio')} value="2" />
Radio3
<input type="radio" {...register('radio')} value="3" />
{errors.radio && <p>radio error</p>}
<input type="checkbox" {...register('checkbox')} />
{errors.checkbox && <p>checkbox error</p>}
<button>Submit</button>
<div id="renderCount">{renderCounter}</div>
</form>
);
};
export default BasicSchemaValidation;
|