Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

190
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda