skip to Main Content

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


  1. Chosen as BEST ANSWER

    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:

    {...register("mailOrNumber")}
    

    this is passed as

    {..register("name")} directly on the <input> tag  
                                                                                                                  
    

    I did like this:

      <MyInput
               type = "text"
               className = {inputClass.join(' ')}
               placeholder = "email или номер телефона"
               defaultValue = ''
               register = {{...register("mailOrNumber")}}
        />  
    
                                                                                                                          
    

  2. I guess you are missing the name attribute on your MyInput component. I am assuming here that inside the MyInput component you are using the standard input controls. You will need to pass the required props from your MyInput component to the underlying input fields. Can you please try something like this?

    <MyInput
        type = "text"
        name = "mailOrNumber"
        className = {inputClass.join(' ')}
        placeholder = "email или номер телефона"
        {...register("mailOrNumber")}
    /> 
    
    <MyInput
      type ={!stateShowPassword ? "password" : "text"}
      name = "password"
      className = {mailEnter.password.length ? "popup- 
      login__input popup-login__input_password hidden" : "popup- 
      login__input popup-login__input_password" }
      placeholder =  "password"
      {...register("password")}
    />
    

    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 value available for both the form validation and submission.

    Note: Each field is required to have a name as a key for the registration process.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search