2

I'm using angular 2 ReactiveFomrs and i have 2 dates in my form the second one calculated from the first one so I make onclick function on the first one and i change the second one using patchValue but it doesn't change

here is the onclick function

onDeliveryDateChange() {
  this.serviceForm.patchValue({
    endDate: "2018-02-31"
  })
}

and here is the html form input fields

 <div class="form-row">
        <div class="form-group col-6">
          <div class="row">
            <label class="col-form-label col-4" for="deliveryDate">Delivery Date</label>
            <input
              type="date"
              id="deliveryDate"
              class="form-control col-7"
              formControlName="deliveryDate"
              (change)="onDeliveryDateChange()">
            <p
              *ngIf="serviceForm.get('deliveryDate').invalid && 
                      serviceForm.get('deliveryDate').touched && 
                      serviceForm.get('deliveryDate').errors['required']"
              class="text-danger col-7 offset-4">
              Delivery Date Can not be empty
            </p>
            <p
              *ngIf="serviceForm.get('deliveryDate').invalid && 
                      serviceForm.get('deliveryDate').touched && 
                      serviceForm.get('deliveryDate').errors['minDate']"
              class="text-danger col-7 offset-4">
              Please don't choose the date before {{currentDate|date}}.
            </p>
          </div>
        </div>
      </div>
      <div class="">
        <div class="form-group col-6">
          <div class="row">
            <label class="col-form-label col-4" for="endDate">End Date</label>
            <input
              type="date"
              id="endDate"
              class="form-control col-7"
              formControlName="endDate"
              [readOnly]="serviceForm.get('contractTerm').value != 4">
            <p
              *ngIf="serviceForm.get('endDate').invalid && 
                      serviceForm.get('endDate').touched && 
                      serviceForm.get('endDate').errors['required']"
              class="text-danger col-7 offset-4">
              End Date Can not be empty
            </p>
            <p
              *ngIf="serviceForm.get('endDate').invalid && 
                      serviceForm.get('endDate').touched && 
                      serviceForm.get('endDate').errors['minDate']"
              class="text-danger col-7 offset-4">
              Please choose the date after {{serviceForm.get('deliveryDate').value}}.
            </p>
          </div>
        </div>
      </div>

note: I also tried to change the format of the date and also tried to make it object of type Date but it didn't change too

Khaled Ahmed
  • 1,074
  • 8
  • 21

1 Answers1

0

If you are using Reactive Forms, why not make it reactive?

  sub: Subscription;

  ngOnInit() {
    const deliveryDate: FormControl = this.serviceForm.get('deliveryDate');
    const endDate: FormControl = this.serviceForm.get('endDate');

    this.sub = deliveryDate.valueChanges.subscribe(v => {
      endDate.setValue(v)
      // or endDate.setValue("2018-02-31")
      // or anything you want
    });
  }

  ngOnDestroy() {
    this.sub.unsubscribe();
  }
Tomasz Kula
  • 16,199
  • 2
  • 68
  • 79