0

I've noticed that when I try to update a couple values in my FormBuilder right before submitting it with HTTPclient my values aren't updated. When I run the submit a 2nd time these values are updated.

private mergeDates(dateValue: string, timeValue: string): string {
  const returnValue = dateValue.toString().replace(' 00:00', ` ${timeValue}`);
  return returnValue;
}

private submitVacancy() {
  if (this.vacancyForm.invalid) {
    return;
  }
  const fValue = this.vacancyForm.value;
  const fControls = this.vacancyForm.controls;
  fControls['beginDateTime'].setValue(
    this.mergeDates(fValue['beginDate'], fValue['beginTime']),
  );
  fControls['endDateTime'].setValue(
    this.mergeDates(fValue['beginDate'], fValue['endTime']),
  );
  alert(JSON.stringify(fValue));
  this.http.post(`${this.apiUri}/addvacancy`, JSON.stringify(fValue));
}
4
  • HTML part please ? Commented Nov 6, 2018 at 14:54
  • what shows you the JSON.stringify(fValue) ? Commented Nov 6, 2018 at 14:56
  • 1
    You're assigning form value to fValue variable before calling setValue method on form controls. Try to move this assignment just above the line with alert function call. Commented Nov 6, 2018 at 14:59
  • @MateuszWitkowski I tried you suggestion and it worked like a charm. Thanks for your help :) Commented Nov 7, 2018 at 13:13

1 Answer 1

1

I added the following line to my code like Mateusz suggested and it now works fine. My code now looks like this.

private mergeDates(dateValue: string, timeValue: string): string {
  const returnValue = dateValue.toString().replace(' 00:00', ` ${timeValue}`);
  return returnValue;
}
private submitVacancy() {
  if (this.vacancyForm.invalid) {
    return;
  }

  const fControls = this.vacancyForm.controls;
  let fValue = this.vacancyForm.value;

  fControls['beginDateTime'].setValue(
    this.mergeDates(fValue['beginDate'], fValue['beginTime']),
  );

  fControls['endDateTime'].setValue(
    this.mergeDates(fValue['beginDate'], fValue['endTime']),
  );

  fValue = this.vacancyForm.value;
  alert(JSON.stringify(fValue));
  console.log(JSON.stringify(fValue));
  this.http.post(`${this.apiUri}/vacancy`, JSON.stringify(fValue));
}

I added fValue = this.vacancyForm.value; right after I run my setValue().

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.