欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Angular實(shí)現(xiàn)模版驅(qū)動(dòng)表單的自定義校驗(yàn)功能(密碼確認(rèn)為例)

 更新時(shí)間:2018年05月17日 08:45:38   作者:Shapeying  
這篇文章主要介紹了Angular實(shí)現(xiàn)模版驅(qū)動(dòng)表單的自定義校驗(yàn)功能(密碼確認(rèn)為例),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

HTML5原生的表單校驗(yàn)屬性(必填,長度限制,取值間隔,正則表達(dá)式等等)可以滿足普通的校驗(yàn)需求,但是有些場景必須用到自定義校驗(yàn),比如注冊時(shí)的密碼確認(rèn),有比對關(guān)系的時(shí)間/數(shù)值選擇, 需要到請求到服務(wù)端取值驗(yàn)證等等···這里以密碼確認(rèn)為例進(jìn)行說明。

指令開發(fā)

  表單的驗(yàn)證狀態(tài)是通過 formContro l的 errors 屬性反饋出來的,所以基本的思路肯定就是需要添加校驗(yàn)規(guī)則,然后將驗(yàn)證結(jié)果添加到formControl實(shí)例的errors屬性中。那么問題來了,模版驅(qū)動(dòng)表單的控制都是在HTML模版中完成的,無法直接接觸到 formControl實(shí)例。這個(gè)時(shí)候就需要使用指令了,將檢驗(yàn)規(guī)則進(jìn)行包裝。Angular提供了 驗(yàn)證器供應(yīng)商 NG_VALIDATORS ,用于處理表單自定義校驗(yàn)。先創(chuàng)建指令。

import { Directive} from '@angular/core';
import { NG_VALIDATORS, Validator, AbstractControl} from '@angular/forms';
@Directive({
 selector: '[appConfirmpsw]',
 providers: [{
  provide : NG_VALIDATORS,
  useExisting : ConfirmpswDirective,
  multi: true
 }]
})
export class ConfirmpswDirective implements Validator {
 constructor() {
  }
 validate(control: AbstractControl): {[key: string]: any} {
   //檢驗(yàn)規(guī)則
 }
}

  1、為指令指定供應(yīng)商  NG_VALIDATORS , 和別名類 ConfirmpswDirective , 及 multi 為true(可以用同一個(gè)token,注冊不同的 provide)。因?yàn)槭窃?NG_VALIDATORS 提供商中注冊的指令,所以才能被Angular的驗(yàn)證流程識別,需要注意的是要用useExisting來注冊,這樣就不會(huì)創(chuàng)建一個(gè)新的實(shí)例。

  2、用 Validator接口來約束 自定義的指令,這是Angular提供的驗(yàn)證器的類 。有validate屬性,會(huì)傳入表單的formControl,返回 ValidationErrors 對象。

  現(xiàn)在指令結(jié)構(gòu)完成,開始進(jìn)行校驗(yàn)部分。首先需要傳入已輸入的密碼,所以增加@input,再指定校驗(yàn)規(guī)則,判斷綁定表單的值和傳入的已輸入值是否相同

@Input('appConfirmpsw') confirmpsw: string;

  為了避免使用指令時(shí),還需要額外傳入confirmpsw屬性 ( <input type="password" appConfirmpsw  [confirmpsw]="'xxx'" >),所以我們將 指令名稱appConfirmpsw作為confirmpsw的別名,這樣傳值會(huì)比較方便,簡化為  <input type="password" [appConfirmpsw] = "'xxx'">

  這里專門寫一個(gè)檢驗(yàn)函數(shù),用來比對值和返回結(jié)果。記得在指令的validate中調(diào)用一下

export function comfirmPswValidator(_confirmpsw: string): ValidatorFn { //傳入已輸入的密碼值 , 返回一個(gè)ValidatorFn
 return (control: AbstractControl): {[key: string]: any} => { //傳入綁定表單的formControl
  if ( !control.value ) { //如果綁定未輸入值,則返回 required錯(cuò)誤
   return { 'required' : true };
  }
  //如果兩次輸入的值不相同,則返回confirmpsw的錯(cuò)誤
  return control.value !== _confirmpsw ? {'confirmpsw' : {value: true}} : null;
 };
}

  完整指令如下:

import { Directive, Input } from '@angular/core';
import { NG_VALIDATORS, Validator, AbstractControl, ValidatorFn} from '@angular/forms';

@Directive({
 selector: '[appConfirmpsw]',
 providers: [{
  provide : NG_VALIDATORS,
  useExisting : ConfirmpswDirective,
  multi: true
 }]
})
export class ConfirmpswDirective implements Validator {

 @Input('appConfirmpsw') confirmpsw: string;
 constructor() {

  }
 validate(control: AbstractControl): {[key: string]: any} {
  console.log(this.confirmpsw);
  return this.confirmpsw ? comfirmPswValidator(this.confirmpsw)(control) : null;
 }
}

export function comfirmPswValidator(_confirmpsw: string): ValidatorFn {
 return (control: AbstractControl): {[key: string]: any} => {
  if ( !control.value ) {
   return { 'required' : true };
  }
  return control.value !== _confirmpsw ? {'confirmpsw' : {value: true}} : null;
 };
}

 使用

  測試一下指令的效果吧

 <div class="input-group">
   <label class="group-label" for="psw-new">&emsp;新密碼 :</label>
   <input class="group-input" [(ngModel)]="inputpsw.new" #new="ngModel" type="password" name="psw" id="psw-new" required>
  </div>
  <div class="input-group input-error" *ngIf="new.touched&&new.invalid">
   <div class="group-error-content" *ngIf="new.errors?.required">確認(rèn)密碼為必填項(xiàng)!</div>
  </div>
  <div class="input-group">
   <label class="group-label" for="psw-confirm">確認(rèn)密碼 :</label>
   <input class="group-input" [(ngModel)]="inputpsw.confirm" #confirm="ngModel" type="password" name="confirm" id="psw-confirm"
   [appConfirmpsw] = "new.value" required>
  </div>
  <div class="input-group input-error" *ngIf="confirm.touched&&confirm.invalid">
   <div class="group-error-content" *ngIf="confirm.errors?.required">新密碼為必填項(xiàng)!</div>
   <div class="group-error-content" *ngIf="confirm.errors?.confirmpsw">密碼輸入不一致!</div>
  </div>

  傳入new表單的值,并通過errors.confirmpsw屬性來控制提示語反饋。密碼輸入不一致,可以正確的校驗(yàn)到

  確認(rèn)密碼為空時(shí)的提示也正確

總結(jié)

以上所述是小編給大家介紹的Angular實(shí)現(xiàn)模版驅(qū)動(dòng)表單的自定義校驗(yàn)功能(密碼確認(rèn)為例),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論