Skip to content

Commit

Permalink
fix: mark all as touched on form components
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoninoBonanno authored Mar 15, 2023
1 parent 1e42f4a commit 05f2d72
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,26 @@ export abstract class AbstractFormComponent<T = any> extends AbstractComponent i
* Fired to check if form control is touched
*/
ngDoCheck() {
if (this.control.touched) {
if (!this._ngControl?.control) {
return;
}
if (this._ngControl?.control?.touched) {
this.control.markAsTouched();

const ngControl = this._ngControl.control;
if (this.control.touched !== ngControl.touched) {
if (ngControl.touched) {
this.control.markAsTouched();
} else {
this.control.markAsUntouched();
}
this._changeDetectorRef.detectChanges();
}
if (this.control.pristine !== ngControl.pristine) {
if (ngControl.pristine) {
this.control.markAsPristine();
} else {
this.control.markAsDirty();
}
this._changeDetectorRef.detectChanges();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ <h3>Interazione con Form Input dotato di validazione Model Driven</h3>
</div>
</div>

<button itButton="primary" [disabled]="!myForm.valid" (click)="save(myForm)">Salva</button>
<div class="d-flex justify-content-between">
<button itButton="primary" [disabled]="!myForm.valid" (click)="save(myForm)">Salva</button>
<button itButton="secondary" type="button" (click)="markAllAsTouched()">Valida</button>
</div>

<div *ngIf="savedValue">Salvato `{{savedValue}}`</div>
</form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ export class ModelDrivenValidationExampleComponent {
this.savedValue = form.value.myInput;
}

markAllAsTouched() {
this.myForm.markAllAsTouched();
}
}

1 comment on commit 05f2d72

@vercel
Copy link

@vercel vercel bot commented on 05f2d72 Mar 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.