带参数的自定义验证
Custom Validation with parameter
我正在使用带 Angular 2 beta 11 的 Ionic 2。
我有一个完美运行的自定义验证器。但是,我现在需要添加另一个自定义验证方法,但似乎无法弄清楚如何将参数传递给它。
如下所示,我有一个自定义验证函数:
ValidationService.userNameExists(control, this.employeeService)
'control'不存在
如何将控件 (FormControl) 传递给 userNameExists 函数?或者,我需要传递字段值(用户名的值)。
谢谢
register.ts
constructor(private nav: NavController, private builder: FormBuilder, employeeService: EmployeeService) {
this.employeeService = employeeService;
this.registerForm = builder.group({
'username': ['', [Validators.required, Validators.minLength(5), Validators.maxLength(55), ValidationService.userNameValidator, ValidationService.userNameExists(control, this.employeeService)]],
'password1': ['', [Validators.required, Validators.minLength(5), Validators.maxLength(45), ValidationService.passwordValidator]],
'password2': ['', [Validators.required, Validators.minLength(5), Validators.maxLength(45), ValidationService.passwordValidator]]
}, { 'validator': ValidationService.matchPassword('password1', 'password2') }
);
}
'control'不存在
ValidationService.ts
public static userNameExists(control: FormControl, employeeService: EmployeeService): any {
return (control: FormControl) => {
let userNameInput = control.value;
console.log('userNameExists: '+control.value);
let promise: Promise<EmployeeModel> = employeeService.getEmployeByUserName(userNameInput);
promise.then((employeeModel: EmployeeModel) => {
if (employeeModel.userName === userNameInput) {
return { userNameExists: true };
} else {
return null;
}
});
}
}
请记住,当您实例化 FormControl/FormGroup 时,您是在传递验证函数,而不是调用它。更改您的用户名声明如下:
'username': ['',
[Validators.required,
Validators.minLength(5),
Validators.maxLength(55),
ValidationService.userNameValidator,
(control) => ValidationService.userNameExists(control, this.employeeService)]]
我正在使用带 Angular 2 beta 11 的 Ionic 2。
我有一个完美运行的自定义验证器。但是,我现在需要添加另一个自定义验证方法,但似乎无法弄清楚如何将参数传递给它。
如下所示,我有一个自定义验证函数:
ValidationService.userNameExists(control, this.employeeService)
'control'不存在
如何将控件 (FormControl) 传递给 userNameExists 函数?或者,我需要传递字段值(用户名的值)。
谢谢
register.ts
constructor(private nav: NavController, private builder: FormBuilder, employeeService: EmployeeService) {
this.employeeService = employeeService;
this.registerForm = builder.group({
'username': ['', [Validators.required, Validators.minLength(5), Validators.maxLength(55), ValidationService.userNameValidator, ValidationService.userNameExists(control, this.employeeService)]],
'password1': ['', [Validators.required, Validators.minLength(5), Validators.maxLength(45), ValidationService.passwordValidator]],
'password2': ['', [Validators.required, Validators.minLength(5), Validators.maxLength(45), ValidationService.passwordValidator]]
}, { 'validator': ValidationService.matchPassword('password1', 'password2') }
);
}
'control'不存在
ValidationService.ts
public static userNameExists(control: FormControl, employeeService: EmployeeService): any {
return (control: FormControl) => {
let userNameInput = control.value;
console.log('userNameExists: '+control.value);
let promise: Promise<EmployeeModel> = employeeService.getEmployeByUserName(userNameInput);
promise.then((employeeModel: EmployeeModel) => {
if (employeeModel.userName === userNameInput) {
return { userNameExists: true };
} else {
return null;
}
});
}
}
请记住,当您实例化 FormControl/FormGroup 时,您是在传递验证函数,而不是调用它。更改您的用户名声明如下:
'username': ['',
[Validators.required,
Validators.minLength(5),
Validators.maxLength(55),
ValidationService.userNameValidator,
(control) => ValidationService.userNameExists(control, this.employeeService)]]