1. 程式人生 > 程式設計 >Angular8 簡單表單驗證的實現示例

Angular8 簡單表單驗證的實現示例

簡單表單校驗

傻瓜式校驗

直接複製Antd中demo

<form nz-form [nzLayout]="'inline'" [formGroup]="validateForm" (ngSubmit)="submitForm()">
 <nz-form-item>
  <nz-form-control nzErrorTip="Please input your username!">
   <nz-input-group nzPrefixIcon="user">
    <input formControlName="userName" nz-input placeholder="Username" />
   </nz-input-group>
  </nz-form-control>
 </nz-form-item>
 <nz-form-item>
  <nz-form-control nzErrorTip="Please input your Password!">
   <nz-input-group nzPrefixIcon="lock">
    <input formControlName="password" nz-input type="password" placeholder="Password" />
   </nz-input-group>
  </nz-form-control>
 </nz-form-item>
 <nz-form-item>
  <nz-form-control>
   <button nz-button nzType="primary" [disabled]="!validateForm.valid">Log in</button>
  </nz-form-control>
 </nz-form-item>
</form>
validateForm!: FormGroup;

 submitForm(): void {
  for (const i in this.validateForm.controls) {
   this.validateForm.controls[i].markAsDirty();
   this.validateForm.controls[i].updateValueAndValidity();
  }
 }

 constructor(private fb: FormBuilder) {}

 ngOnInit(): void {
  this.validateForm = this.fb.group({
   userName: [null,[Validators.required]],password: [null,remember: [true]
  });
 }

此類表單校驗較為單一,或者要求比較低,通常即為required校驗,錯誤資訊提示也多為固定不變。

Angular8 簡單表單驗證的實現示例

智慧化校驗

表單一旦開始有具體的細節校驗或者複雜的業務參雜,校驗提示必須準確、清晰。
userName為例,除了為必填項,必然需要符合某種格式,亦或是指定郵箱格式等,那麼校驗必須同時反映出錯誤型別,本例假設使用者名稱有長度要求進行演示。

<nz-form-control [nzErrorTip]="getErrTipByField('userName')">
   <nz-input-group nzPrefixIcon="user">
    <input formControlName="userName" nz-input placeholder="Username" />
   </nz-input-group>
  </nz-form-control>
export class SimpleFormComponent implements OnInit {
 validateForm: FormGroup;
 errMessageMap = {};

 constructor(
  private fb: FormBuilder
 ) { }

 ngOnInit() {
  this.errMessageMap = {
   userName: {
    required: 'please input your name!',minlength: 'please input at least least 5 character'
   },password: {
    required: 'please input your password!'
   }
  };
  this.validateForm = this.fb.group({
   userName: [null,[Validators.required,Validators.minLength(5)]],remember: [true]
  },{ updateOn: 'change' });
 }
 submitForm(): void {
  if (this.validateForm.controls) {
   for (const i in this.validateForm.controls) {
    if (this.validateForm.controls[i]) {
     this.validateForm.controls[i].markAsDirty();
     this.validateForm.controls[i].updateValueAndValidity();
    }
   }
  }
 }
 getErrTipByField(fieldName) {
  const errors = this.validateForm.get(fieldName).errors;
  let errMsg = '';
  for (const key in errors) {
   if (errors.hasOwnProperty(key)) {
    errMsg += this.errMessageMap[fieldName][key];
   }
  }
  return errMsg;
 }

}

構建出一個數據物件,以滿足不同欄位下不同錯誤型別的錯誤提示,根據業務需求,決定是否顯示全部錯誤資訊或者按照業務優先順序顯示。

Angular8 簡單表單驗證的實現示例

其它細節

表單校驗時機可以設定,預設為change,可選blursubmit

表單驗證正確的,想要提示勾號可以新增屬性 nzHasFeedback

Angular8 簡單表單驗證的實現示例

submit中那段程式碼是重新對錶單進行驗證了,一般驗證中是不需要的,動態表單驗證後續結合自定義表單服務進行演示。

到此這篇關於Angular8 簡單表單驗證的實現示例的文章就介紹到這了,更多相關Angular8 表單驗證內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!