Angular實(shí)現(xiàn)模版驅(qū)動(dòng)表單的自定義校驗(yàn)功能(密碼確認(rèn)為例)
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"> 新密碼 :</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)文章
Angular動(dòng)畫實(shí)現(xiàn)的2種方式以及添加購物車動(dòng)畫實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于Angular動(dòng)畫的2種方式以及添加購物車動(dòng)畫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08AngularJS constant和value區(qū)別詳解
angularJS可以通過constant(name,value)和value(name,value)對于創(chuàng)建服務(wù)也是很重要的。他們之間有什么不同呢?今天小編給大家分享AngularJS constant和value區(qū)別詳解,需要的朋友參考下2017-02-02angularjs實(shí)現(xiàn)多選框分段全選效果實(shí)現(xiàn)
這篇文章主要為大家介紹了angularjs實(shí)現(xiàn)多選框分段全選效果實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06從源碼看angular/material2 中 dialog模塊的實(shí)現(xiàn)方法
這篇文章主要介紹了 從源碼看angular/material2 中 dialog模塊的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-10-10Angularjs 實(shí)現(xiàn)移動(dòng)端在線測評效果(推薦)
這篇文章主要介紹了Angularjs 實(shí)現(xiàn)移動(dòng)端在線測評效果,需要的朋友可以參考下2017-04-04angular實(shí)現(xiàn)IM聊天圖片發(fā)送實(shí)例
本篇文章主要介紹了angular實(shí)現(xiàn)IM聊天圖片發(fā)送實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05AngularJs ng-repeat 嵌套如何獲取外層$index
這篇文章主要介紹了AngularJs ng-repeat 嵌套如何獲取外層$index的相關(guān)資料,需要的朋友可以參考下2016-09-09