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

245
Visualizações
How to prevent sending empty values of form when submitting?

I have an Ant form in a React app, where some fields are required and some not. My problem is that when not required field is empty, backend sends the validation error. My question is, is there a way to NOT TO SEND the value of not required field if there is no value?

Screenshot of the current object that is sent:

enter image description here

My code for form submission:

  const submitHandler = async (val: any) => {
    setLoading(true);
    try {
      let res: any = await API.put(`recipients/${pid}`, {
        ...val,
        user_id: loggedInUser.id,
        type: type,
      });
      setLoading(false);
      message.success("Benutzer erfolgreich aktualisiert");
      setError("");
    } catch (error: any) {
      setLoading(false);
      setError(error?.response.data.message);
      message.error(error?.response.data.message);
    }
  };
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

idk about typescript, but this is how i would do in javascript:

const submitHandler = async (val: any) => {
    ...
    for (let key in val) {
        if(!val[key]) delete val[key];
    }
    ...
};
about 4 years ago · Juan Pablo Isaza Relatório

0

You can remove properties with empty value from that val before sending it to the back end. A way is doing it so:

let res = await API.put(`recipients/${pid}`, {
  ...Object.fromEntries(Object.entries(val).filter(([_, v]) => v)),
  user_id: loggedInUser.id,
  type: type,
});

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