Reputation: 3816
I'm using react-hook-form library and yup to validate input fields :
const { handleSubmit, register, errors } = useForm({
mode: 'onBlur',
validationSchema: Yup.object({
name: Yup.string().max(6, 'Max 6 chars').required('Required boy'),
pass: Yup.string().min(6, 'Min 6 chars').required('Required boy')
})
});
const submit = (e) => {
alert(e.name + ' ' + e.pass);
};
return (
<div className="App">
<form onSubmit={handleSubmit(submit)}>
<input id="name" type="text" name="name" ref={register} />
{errors.name && <div>{errors.name.message}</div>}
<input id="pass" type="password" name="pass" ref={register} />
{errors.pass && <h3>{errors.pass.message}</h3>}
<button type="submit">Submit</button>
</form>
</div>
);
It doesn't throw an error in the console and it alerts when I click submit button but the validation doesn't work at all .
I expect the error message to show up when the input is touched and the value is less or more than the required value .
How can I use yup properly with react-hook-form ?
Upvotes: 1
Views: 13264
Reputation: 389
phone: yup.string().length(11,'11 digit require').required().label('Phone'),
Upvotes: 0
Reputation: 351
You can use the latest version without downgrading it to 4.9.8
The documentation is in advance section of react hook form under "Custom Hook with Resolver" https://react-hook-form.com/advanced-usage
my sample code below: https://codesandbox.io/s/react-hook-form-yup-material-ui-8ino9
Upvotes: 2