3
我想檢查兩個密碼是否相等。我沒有收到任何錯誤消息,但沒有收到錯誤消息「密碼必須匹配」。在角度2中的密碼確認
下面是我的DOM:
<div class="form-group" [ngClass]="{'has-error': formName.get('password').touched &&
formName.get('password').hasError('invalidPassword')}">
<label class="control-label">Password</label>
<input type="text" class="form-control" formControlName="password" name="password" required />
</div>
<div class="form-group" [ngClass]="{'has-error': formName.get('confirmpassword').touched
&& formName.get('confirmpassword').hasError('mismatchedPasswords')}">
<label class="control-label">confirm password</label>
<input type="text" class="form-control" formControlName="confirmpassword" name="confirmpassword" required />
<span class="help-block" *ngIf="formName.get('confirmpassword').touched
&& formName.get('confirmpassword').hasError('mismatchedPasswords')">
Password must match
</span>
</div>
我對我是多麼建設我的表格組件類:
this.formName = this.fb.group({
name: ["", [Validators.required]],
password: ["",[Validators.required, ValidationHelper.passwordValidator]],
confirmpassword: ["",Validators.required],
info: this.fb.group({
acc: ["",[Validators.required, ValidationHelper.creditCardValidator]]
})
},{ validator: ValidationHelper.matchPass})
我的密碼匹配功能:
static matchPass = (control: AbstractControl) : {[key: string]: boolean} => {
let password = control.get('password');
let confirmPassword = control.get('confirmpassword');
return password.value === confirmPassword.value ? null : { 'mismatchedPasswords': true };
}
功能是被叫,我也得到了返回值...但爲什麼我的確認密碼輸入控件不顯示錯誤或a激活其span標籤。