I want to do validation for my form before it will send to server, but I have a problem with useForm.
const {
register, handleSubmit, setError, formState: { errors, isValid }
} = useForm<loginFor>()
const onSubmit: SubmitHandler<loginFor> = data => console.log(data);
My component where I try to do validation.
In my inputs I added {...register("password")}
and {...register("mailOrNumber")}
.
In my form I added onSubmit={handleSubmit(onSubmit)}
.
But when I write something in input and then I click on button with type = submit
I see at console browser this:
{mailOrNumber: undefined, password: undefined}
Why undefined
?
API documentation for react-hook-form suggests that it must work.
return (
<div className="popup-login">
<div className="popup-login__cover-close-btn">
<MyButton
className="popup-login__close"
type="button"
onClick={changeStateForPopupClose}
></MyButton>
</div>
<div className="popup-login__main-cover">
<form onSubmit={handleSubmit(onSubmit)}>
<div className="popup-login__cover-block">
<h2 className="popup-payment__title">
{
enterOrRegister === 'вход' ? "Вход в учетную запись" :
"Регистрация"
}
</h2>
<div className="popup-login__enter">
<MyInput
type="text"
className={inputClass.join(' ')}
placeholder="email или номер телефона"
{...register("mailOrNumber")}
/>
<div className="popup-login__for-btn-show-hide">
<MyInput
type={!stateShowPassword ? "password" : "text"}
className={mailEnter.password.length ? "popup-
login__input popup-login__input_password hidden" : "popup-
login__input popup-login__input_password" }
placeholder = "password"
{...register("password")}
/>
{
mailEnter.password.length != 0 &&
<div
className="popup-login__for-btn-show-hide_btn"
onClick={showPassword}
>
{!stateShowPassword ? "SHOW" : "HIDE"}
</div>
}
</div>
{
enterOrRegister === 'вход' && <MyCheckbox />
}
</div>
{enterOrRegister === 'вход' &&
<div className="popup-login__additional">
<h2 className="popup-login__titile-register">Нет аккаунта?
</h2>
<MyButton
className="popup-login__register"
type="button"
onClick={changeFormRegister}
>
зарегистрироваться
</MyButton>
</div>
}
</div>
<div className="popup-login__cover-btns">
<MyButton
className="popup-login__btn"
type="submit"
// onClick = {clickedOnEnter}
>
{
enterOrRegister === 'вход' ? "Войти" : "Зарегистрироваться"
}
</MyButton>
</div>
</form>
</div>
</div>
);
};
2
Answers
inkredusk helped me and it's working.
Basically u need to pass as props from MyInput to all things that are required. like for example:
this is passed as
I did like this:
I guess you are missing the
name
attribute on yourMyInput
component. I am assuming here that inside theMyInput
component you are using thestandard input
controls. You will need to pass the requiredprops
from yourMyInput
component to the underlyinginput
fields. Can you please try something like this?UPDATE
As per the React hooks form documentation for registering fields, one of the key concepts in
React Hook Form
is to register your component into the hook. This will make its valueavailable
for both theform validation and submission
.Note: Each field is required to have a
name
as a key for the registration process.