确认密码验证器不起作用 - Angular

时间:2017-09-13 05:24:52

标签: angular

我试过制作一个自定义验证器。如果密码不匹配,我想添加ng-invalid类(将输入设置为无效)。

这是html -

<div class="form-group">
    <label for="password">Password:</label>
    <label class="validations" @load *ngIf="!regForm.get('password').valid && regForm.get('password').touched">Please Enter a Valid Password!</label>
    <input type="password" class="form-control" id="password" minlength="6" required placeholder="Enter Your Team Password" formControlName="password">
</div>
<div class="form-group">
      <label for="password">Confirm Password:</label>
      <label class="validations" @load *ngIf="(!(regForm.get('password').value == regForm.get('password_confirmation').value)) && regForm.get('password_confirmation').touched">Passwords do not Match!</label>
      <input type="password" class="form-control" id="password" 
          [ngClass]="{pwd_mismatch : !(regForm.get('password').value === regForm.get('password_confirmation').value) && regForm.get('password_confirmation').touched}" required placeholder="Re-Enter Your Team Password" formControlName="password_confirmation">
</div>

和component.ts -

passwordConfirm(control: FormControl): {[s: string]: boolean} {
    if (control.value != this.password) {
        control.hasError;
        return null;
    }
    return {'mismatch': true};
}

注意:this.password存储来自密码FormControl的输入。 pwd_mismatch类实时添加到输入中,但未设置为无效

1 个答案:

答案 0 :(得分:2)

你的逻辑是倒退的。如果密码匹配,则必须返回null,即如果没有错误。所以代码应该是

print('For', cookies, 'You would need the following:')
print(format(sugar_calcul, '.2f'),': cups of sugar')
print(format(butter_calcul, '.2f'), ': cups of butter')
print(format(flour_calcul, '.2f'), ': cups of flour')
print('Enjoy Your cookies !')