0

I want to validate my forms before calling the onClick function. I have used react-hook-form component to implement the form-validation. However, it doesn't seem to work. Even when the form is empty if I click on the button an empty component will be created.

I'm a beginner to react. This project is a from a Udemy course and I wanted to make some improvements i.e form-validation.

import React, { useState } from "react";
import AddIcon from "@material-ui/icons/Add";
import Fab from "@material-ui/core/Fab";
import Zoom from "@material-ui/core/Zoom";
import { useForm } from "react-hook-form";

function CreateArea(props) {

  const { register, handleSubmit, errors } = useForm();
  const onSubmit = data => {
    console.log(data);
  };

  const [createNote, setCreateNote] = useState({
    title: "",
    content: "",
  });

  const [isExpanded, setIsExpanded] = useState(false);

  
  function updateChange(event) {
    const { name, value } = event.target;
    setCreateNote((prevNote) => {
      return {
        ...prevNote,
        [name]: value,
      };
    });
  }

  function submitNote(event) {
    props.onAdd(createNote);
    setCreateNote({
      title: "",
      content: "",
    });
    event.preventDefault();
  }

  function expand(){
    setIsExpanded(true);
  }

  return (
    
    <div>
      <form onSubmit={handleSubmit(onSubmit)} className="create-note" >
        {isExpanded && <input
          onChange={updateChange}
          value={createNote.title}
          name="title"
          placeholder="Title"
          ref={register({ required: true ,maxLength: 15 })}
        />}
        {errors.title && <p>Title is required</p>}

        <textarea
          onClick={expand}
          onChange={updateChange}
          value={createNote.content}
          name="content"
          placeholder="Take a note..."
          rows={isExpanded ? "3" : "1"}
          ref={register({ required: true })}
        />
        {errors.content && <p>Content is required</p>}
        
        <Zoom in={isExpanded}>
          <Fab type="submit" onClick={submitNote}>
            <AddIcon />
          </Fab>
        </Zoom>

      </form>
    </div>
  );
}

export default CreateArea;
7
  • Try Removing the onClick from the Fab Component. Its unnecessary. working demo codesandbox Commented Jun 24, 2020 at 6:09
  • I'm using onClick method to pass values to another component. Commented Jun 24, 2020 at 6:13
  • you can do that inside onSubmit function. Commented Jun 24, 2020 at 6:18
  • 2
    you don't even have to maintain local states. react-hook-form will do that for you. your code can be as simple as this Commented Jun 24, 2020 at 6:21
  • Oh okay. It worked. Thanks!! Commented Jun 24, 2020 at 6:30

1 Answer 1

1

I'm not really sure if I understand the question completely, but if you want to validate the form in the expand function, and act on whether it's valid or not, react-hook-form has a triggerValidation function that returns a promise for this purpose.

const { triggerValidation } = useForm();

function expand() {
    triggerValidation().then(valid => {
        if (valid) {
            setIsExpanded(true);
        }
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.