1

I have a reactive form with Datepicker

 <div class="form-group">
     <label class="form-control-label" for="field_dataAtivacao">Data Ativação</label>
     <div class="input-group">
         <input id="field_dataAtivacao" type="text" class="form-control" name="dataAtivacao" ngbDatepicker #dataAtivacaoDp="ngbDatepicker" formControlName="dataAtivacao"/>
         <span class="input-group-append">
             <button type="button" class="btn btn-secondary" (click)="dataAtivacaoDp.toggle()">
                 <fa-icon [icon]="'calendar-alt'"></fa-icon>
             </button>
         </span>
     </div>
 </div>

Form control without validator

editForm = this.fb.group({
    dataAtivacao: null,
});

The field is empty but always is required. I tried this solution but without success

EDIT

The above example works normally when I don't use the patch value method to update the form.

Like this example of the comments

The problem occurs when I need to update the form using the patchValue method.

this.editForm.patchValue({
    dataAtivacao: cliente.dataAtivacao,
});

When I change for null works normally but I need to show the date and allows update the form with the current selected

this.editForm.patchValue({
        dataAtivacao: null,
});

https://stackblitz.com/edit/angular-grybsk-vrnclb

renanvm
  • 208
  • 3
  • 16
  • Your code should work perfectly fine: https://stackblitz.com/edit/angular-grybsk?file=app/datepicker-basic.html Please provide a [mcve]. **Please also note to not use `ngModel` with formcontrol**. In my stackblitz it doesn't seem to cause any issue, but maybe on your end. – AT82 Oct 30 '19 at 12:48
  • I've identified the problem. It occurs when I use the patchValue method of form group. I will edit the question – renanvm Oct 30 '19 at 16:04
  • So fork my stackblitz to showcase the issue. I don't understand why patchValue would cause such an issue? – AT82 Oct 30 '19 at 16:59
  • https://stackblitz.com/edit/angular-grybsk-vrnclb – renanvm Oct 30 '19 at 17:33

1 Answers1

1

Use this way

editForm = this.fb.group({
    dataAtivacao : ["", Validators.required]
})
dasunse
  • 2,839
  • 1
  • 14
  • 32