我似乎无法弄清楚为什么我的自定义验证器比字段值落后一步。示例:我的输入字段的值 123 是一一键入的。但我的验证器的值为 12。
我无法正确比较两个字段之间的值。这是指令中的验证器:
@Directive({
selector: '[validateEqual][formControlName],[validateEqual][formControl],[validateEqual][ngModel]',
providers: [
{
provide: NG_VALIDATORS,
useExisting: forwardRef(() => PasswordValidationDirective),
multi: true
}
]
})
export class PasswordValidationDirective implements Validator {
@Input('first') first: string;
@Input('second') second: string;
constructor() {
}
public validate(ac: AbstractControl): { [key: string]: any } {
console.log(ac.root);
return null;
}
}
这是该字段的html:
<md-input-container class="full-width">
<input mdInput
type="password"
required
ngModel name="passwordConfirmation"
#passwordConfirmation="ngModel"
minlength="6"
maxlength="30"
pattern="(?=^.{6,30}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$"
validateEqual
first="password"
second="passwordConfirmation"
placeholder="{{'PASSWORD_RECOVERY.PASSWORD_CONFIRMATION' | translate}}">
<md-error *ngIf="passwordConfirmation.touched && passwordConfirmation.invalid">
<span *ngIf="passwordConfirmation.errors.required">
{{'PASSWORD_RECOVERY.FIELD_REQUIRED' | translate}}
</span>
<span *ngIf="passwordConfirmation.errors.minlength || passwordConfirmation.errors.maxlength">
{{'PASSWORD_RECOVERY.PASSWORD_LENGTH' | translate}}
</span>
<span *ngIf="passwordConfirmation.errors.pattern" class="p-md-error-multiline-div">
{{'PASSWORD_RECOVERY.FOR_A_SECURE_PASSWORD' | translate}}
</span>
</md-error>
</md-input-container>