Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度(4):密码匹配验证程序不工作_Angular_Angular Validation - Fatal编程技术网

Angular 角度(4):密码匹配验证程序不工作

Angular 角度(4):密码匹配验证程序不工作,angular,angular-validation,Angular,Angular Validation,我正试图为Angular(4)编写我的第一个自定义验证器。我想我把一切都设置好了,但是缺少了一些东西。验证器根本不工作。它没有显示任何错误或任何东西 以下是验证程序: export class PasswordValidator { static matchPassword(control: AbstractControl): ValidationErrors | null { const password = control.get('password').value; c

我正试图为Angular(4)编写我的第一个自定义验证器。我想我把一切都设置好了,但是缺少了一些东西。验证器根本不工作。它没有显示任何错误或任何东西

以下是验证程序:

export class PasswordValidator {
  static matchPassword(control: AbstractControl): ValidationErrors | null {
    const password = control.get('password').value;
    const passwordConfirmation = control.get('passwordConfirmation').value;
    if (password != passwordConfirmation) {
      console.log('passwords did not match');
      control.get('confirmPassword').setErrors({matchPassword: true})
    } else {
      console.log('successfully matched');
      return null
    }
  }
}
这是验证指令:

@Directive({
  selector: '[passwordDomain][ngModel]',
  providers: [
    {
      provide: NG_VALIDATORS,
      useValue: PasswordValidator,
      multi: true
    }
  ]
})
export class PasswordValidationDirective {

  constructor() {
  }

}
这是使用该验证器的html代码:

<input mdInput
       type="password"
       required
       ngModel name="passwordConfirmation"
       #passwordConfirmation="ngModel"
       passwordDomain
       placeholder="{{'PASSWORD_RECOVERY.PASSWORD_CONFIRMATION' | translate}}">
       <md-error *ngIf="passwordConfirmation.touched && passwordConfirmation.invalid">
                <span *ngIf="passwordConfirmation.errors?.matchPassword">
                      {{'PASSWORD_RECOVERY.MATCH' | translate}}
                </span>
       </md-error>
</md-input-container>

{{'PASSWORD_RECOVERY.MATCH'| translate}}

读取方法的返回类型。它应该返回ValidationErrors,如果没有错误,则返回null。如果没有错误,您的方法将返回null,如果有错误,则不返回任何内容。相反,它会更新其中一个控件的错误,但它不应该这样做。此外,此验证器需要一个FormGroup作为参数,它包含两个控件password和passwordConfirmation。但是您在输入上添加了指令(即在单个FormControl上)。哦,另外,您在传递类,而不是在指令装饰器中传递验证指令。你的指令没有任何作用。@jbnize你能不能写一个答案?提前谢谢。我已经给你的评论,+文档()中的解释应该已经对你有所帮助。没有必要粗鲁。我想得到一个完整的答案,并按照这个社区的正确流程接受它。它应该返回ValidationErrors,如果没有错误,则返回null。如果没有错误,您的方法将返回null,如果有错误,则不返回任何内容。相反,它会更新其中一个控件的错误,但它不应该这样做。此外,此验证器需要一个FormGroup作为参数,它包含两个控件password和passwordConfirmation。但是您在输入上添加了指令(即在单个FormControl上)。哦,另外,您在传递类,而不是在指令装饰器中传递验证指令。你的指令没有任何作用。@jbnize你能不能写一个答案?提前谢谢。我已经给你的评论,+文档()中的解释应该已经对你有所帮助。没有必要粗鲁。我想得到一个完整的答案,并按照社区的正确流程接受它。