Angular 手动设置角度5中的值后验证反应形式?

Angular 手动设置角度5中的值后验证反应形式?,angular,angular-material,material-design,angular5,angular-reactive-forms,Angular,Angular Material,Material Design,Angular5,Angular Reactive Forms,当某些事件发生时,我正在手动设置一个被动表单。 它们会被更新,尽管它们不会通过任何形式的验证 大概是 this.FORM.controls.WHATEVERINPUT.setValue(X); 重点是,我使用的是材料设计和角度5,所以在模板中,我有这个结构 <mat-form-field> <input matInput formControlName="WHATEVERINPUT"> <mat-error *ngIf="FORM?.controls.WHATEV

当某些事件发生时,我正在手动设置一个被动表单。 它们会被更新,尽管它们不会通过任何形式的验证

大概是

this.FORM.controls.WHATEVERINPUT.setValue(X);
重点是,我使用的是材料设计和角度5,所以在模板中,我有这个结构

<mat-form-field>
<input matInput formControlName="WHATEVERINPUT">
<mat-error *ngIf="FORM?.controls.WHATEVERINPUT.errors">
 ... is required & Minimum of 5 characters
</mat-error>
</mat-form-field>
有几种方法,但我还没有得到一个值已验证的

如果有人能以任何方式帮助我,我将不胜感激
谢谢

编辑:简单回答

您可以调用
this.form.get('username').markAsTouched()

另一个答案

如果您将按钮放在表单中,请将
(ngSubmit)=“USR($event)”
作为属性添加到表单标记中,并在按钮上添加
type=“submit”
,您将进行角度检测,以确定表单是否有效,然后将显示错误消息

<form class="example-form" [formGroup]="form" (ngSubmit)=USR($event)>
  <mat-form-field class="example-full-width">
    <input matInput placeholder="Username" formControlName="username">
  </mat-form-field>

  <mat-form-field class="example-full-width">
    <input matInput placeholder="Password" formControlName="password">
  </mat-form-field>

  <button mat-raised-button type="button" color="primary" [style.margin]="'12px'" (click)="PWD()">Update password with value</button>
  <button type="submit" mat-raised-button color="primary" [style.margin]="'12px'">Update username required error</button>
</form>

使用值更新密码
需要更新用户名错误

您可以发布组件的typescript代码吗?如何创建控制器,以及您使用的是哪种类型的
验证器
<form class="example-form" [formGroup]="form" (ngSubmit)=USR($event)>
  <mat-form-field class="example-full-width">
    <input matInput placeholder="Username" formControlName="username">
  </mat-form-field>

  <mat-form-field class="example-full-width">
    <input matInput placeholder="Password" formControlName="password">
  </mat-form-field>

  <button mat-raised-button type="button" color="primary" [style.margin]="'12px'" (click)="PWD()">Update password with value</button>
  <button type="submit" mat-raised-button color="primary" [style.margin]="'12px'">Update username required error</button>
</form>