4

x/4.x & bootstrap coders,

I used patchValue to fill a control with data but this does not effect the dirty flag, leaving the form pristine. Should behave like this or is this a bug?

The control:

    <div class="form-group has-feedback">
        <label class="col-sm-2 control-label" for="projects">{{ 'projects' | translate }}*</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" formControlName="projects" style="cursor: pointer" (click)="this.clickedShowProjectsForm()" aria-describedby="projects" placeholder="{{ 'choose a project' | translate }}">
            <span class="glyphicon glyphicon-menu-right form-control-feedback" style="color: #007734!important" aria-hidden="true"></span>
        </div>
    </div>

The filling using patchValue:

    this.formGroup.patchValue({
       projectId: id,
       projects: selectedProject
    });

This was supposed to make my buttons react. But is does not... :-(

The buttons are defined as follows:

    <div style="text-align:center">
        <button class="btn btn-success button" (click)="clickedStore();" [disabled]="!(formGroup.valid && !formGroup.pristine)">{{ 'store' | translate }}</button>
        <button class="btn btn-danger button" (click)="clickedCancel();" [disabled]="formGroup.pristine">{{ 'cancel' | translate }}</button>
    </div>

I check stackoverflow and something was mentioned in:

How to programmatically change pristine property in angular 2 form without ng-model

There they manually alter the pristine flag, so I tried the same, using:

    this.formGroup.controls.projects.value.markAsDirty();

It has no effect what so-ever...

So is this a bug or desired behavior?

In my humble opinion patchValue should alter the dirty flag is the contents is changed...

Is this a bug or the intended behavior?

divibisan
  • 11,659
  • 11
  • 40
  • 58
  • I made a Plunker to demonstrate this: https://embed.plnkr.co/nWKCZXH42gmFdBMzndVj/ – Roland Slegers Aug 23 '17 at 08:33
  • 1
    A colleague of mine (thanks Gertjan) mentioned that you can do the following, which indeed sets the whole form to dirty: this.formGroup.markAsDirty(); But I still think this should be done by patchValue, so I am still inclined to say that this is a bug. – Roland Slegers Aug 23 '17 at 09:20
  • On https://stackoverflow.com/questions/39802871/angular2-how-do-you-mark-formgroup-control-dirty-via-patchvalue I found the following: (this.formGroup.controls['projects'] as FormControl).markAsDirty(); This will set only the control to dirty. – Roland Slegers Aug 24 '17 at 02:34

0 Answers0