Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda