Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
Passing multiple functions to onClick prop of a button (validation)

I have a text field to enter a title in a dialog modal. There a button called 'create' that when clicked both adds an item to a table AND closes the dialog modal.

<Button onClick={createProjectButtonHandler} variant="contained">

And the function that handles this is:

  const createProjectButtonHandler = () => {
    props.onSaveProjectData(projectData);
    props.onCloseModal();

    setProjectTitle("");
  };

Here is my title validation function and state:

// title validation state and handler
  const [titleError, setTitleError] = useState(false);

  const validateTitle = (title) => {
    return title === 0 ? setTitleError(true) : setTitleError(false);
  };

Here my input field:

  <TextField
    label="Title"
    variant="outlined"
    required
    onChange={projectTitleHandler}
    error={titleError}
    helperText={titleError && "Please enter a title"}
  ></TextField>

How do I add this validation function to the createProjectButtonHandler function so that these two: props.onSaveProjectData(projectData); props.onCloseModal(); only happen when titleError is false, otherwise when clicking the button and titleError is true I want to show that error.

In short, I want to validate the input text AFTER clicking the button. If no errors, then proceed.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

import { useForm} from "react-hook-form"; 
 const { register, handleSubmit, errors} = useForm();

const submitForm = (data) => {
 props.onSaveProjectData(data);
    props.onCloseModal();

    setProjectTitle("");
}



<form onSubmit={handleSubmit(submitForm)}>
                <Grid item>
                  <TextField
                    name="Title"
                    label="title"
                    type={"text" }
                    ref={register({ required: true })}
                    error={!!errors.Title}
                    helperText="Please enter a title"
                  />
                </Grid>
                <Grid item>
                    <Button type="submit">
                      submit
                    </Button>
                 </Grid>
</form>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!