Hamdy
Hamdy

Reputation: 440

Delete parent field from formBuilder angular 4

I want to delete a parent field from my form

This is my example:

  initForm() {
    return this.myForm= this.formBuilder.group({
      startDate: [this.data.startDate?this.data.startDate:new Date().toISOString(),Validators.compose([Validators.required])],
      dateDeposit: [this.data.dateDeposit?this.data.dateDeposit:new Date().toISOString(),Validators.compose([Validators.required])],
  comment: [this.data.comment?this.data.comment:"",Validators.compose([Validators.required])],
firstName: [this.data.firstName?this.data.comment:"",Validators.compose([Validators.required])],
lastName: [this.data.lastName?this.data.comment:"",Validators.compose([Validators.required])],
adress: [this.data.adress?this.data.comment:"",Validators.compose([Validators.required])],

remarks: [this.data.remarks?this.data.remarks:"",Validators.compose([Validators.required])],

otherRemarks: [this.data.otherRemarks?this.data.otherRemarks:"",Validators.compose([Validators.required])],
contactClient: [this.data.contactClient?this.data.contactClient:"",Validators.compose([Validators.required])],
clientNumber: [this.data.clientNumber?this.data.clientNumber:"",Validators.compose([Validators.required])],
    });
  }

Then I send the this.myForm.value as an object on my HTTP request.

   this.myService.putForm(this.myForm.value).subscribe();

The problem is that I do not want to send dateDeposit. How can I delete DateDeposit from myForm when I send the myForm.value ?

Upvotes: 1

Views: 157

Answers (3)

acdcjunior
acdcjunior

Reputation: 135762

You can send just the startDate. Try:

this.myService.putForm(this.myForm.get('startDate').value).subscribe();

or

this.myService.putForm(this.myForm.controls['startDate'].value).subscribe();

If you mean to use all other values, but dateDeposit, you can:

// get form value without dateDeposit
let formValue = Object.assign({}, this.myForm.value);
delete formValue.dateDeposit;
// submit form value without dateDeposit
this.myService.putForm(formValue).subscribe();

Upvotes: 1

Daniel Netzer
Daniel Netzer

Reputation: 2232

this.myForm.removeControl('depositDate')

https://angular.io/api/forms/FormGroup#removeControl

you can simply remove the control from the form itself, but be aware it might create issues in the template since there is an input tag bound to this formControl

Upvotes: 0

Arun Kumaresh
Arun Kumaresh

Reputation: 6311

simply you can use

this.myService.putForm(this.myForm.value.startDate).subscribe();

Upvotes: 0

Related Questions