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

244
Views
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 answers
Answer question

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 Report

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 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!