I manage to clearErrors with onClick button , but not with onChange on input field , why ? Please help #7333
-
Hello all , 👍 I want to clear the error on a input when the value change so case senario is : I want to change the use case 3/ by So if i do this , error message is clear when i press the button const {register, handleSubmit, reset, clearErrors, formState: { errors,isSubmitting, isSubmitted, isSubmitSuccessful , isDirty}} = useForm({mode : 'onSubmit'}); <div className="form-floating mb-3">
<button
type="button"
onClick={() => clearErrors(["email"])}
>
Clear email error
</button>
<input
name="email"
type="email"
className="form-control"
id="floatingInput"
placeholder={t('common:email_placeholder')}
autoComplete="off"
{...register("email", {
// required: "You need do put an email",
pattern: {
value: /^(([^<>()\[\]\\.,;:\s@"]+/,
message: t('common:email_error'),
}
})}
/> if i do this , error message is still here and doesn't clear after i start typing again , but i call the same function , don't know why it doesn't work properly const {register, handleSubmit, reset, clearErrors, formState: { errors,isSubmitting, isSubmitted, isSubmitSuccessful , isDirty}} = useForm({mode : 'onSubmit'}); <div className="form-floating mb-3">
<button
type="button"
onClick={() => clearErrors(["email"])}
>
Clear email error
</button>
<input
name="email"
type="email"
className="form-control"
id="floatingInput"
placeholder={t('common:email_placeholder')}
autoComplete="off"
onBlur={() => clearErrors(["email"])}
onChange={() => clearErrors(["email"])}
onInput={() => clearErrors(["email"])}
{...register("email", {
// required: "You need do put an email",
pattern: {
value: /^(([^<>()\[\]\\.,;:\s@"]+/,
message: t('common:email_error'),
}
})}
/> |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
https://react-hook-form.com/api/useform/clearerrors
code like above is conflicting with your validation. |
Beta Was this translation helpful? Give feedback.
-
i know clearErrors doesn't affect validation rules and i don't want too so it's perfect. const handleValueChange = (e) => {
clearErrors(e.target.name);
}; <form
name="email"
id="viralLoopForm"
onSubmit={handleSubmit(onSubmit)}
onInput={handleValueChange}
autoComplete="off"
{...register("email", {
pattern: {
value: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
message: t('common:email_error'),
}
})}
>
|
Beta Was this translation helpful? Give feedback.
-
find a working solution @bluebill1049 many thanks for your help :) 👍 |
Beta Was this translation helpful? Give feedback.
-
JAGAJ |
Beta Was this translation helpful? Give feedback.
find a working solution @bluebill1049 many thanks for your help :) 👍
i share it here : https://codesandbox.io/s/cranky-ride-hm313?file=/src/App.js