Angular2:用于确认密码的模型驱动表单验证
Angular2: Model driven form validation for confirm password
我们如何使用angular2验证来确认密码?我正在使用 angular 2.0 final。
我正在使用 formbuilder class 用于模型驱动的表单,如下所示:
this.myForm = this.fb.group({
name: ['', Validators.compose([Validators.required])],
email: [''],
passwordgroup: this.fb.group({
password: ['', Validators.compose([Validators.required])],
confirm_password: ['', Validators.compose([])]
})
});
您可以像这样将此函数添加到您的组件中,从而简单地使用自定义验证器
import { AbstractControl } from '@angular/forms';
function passwordConfirming(c: AbstractControl) {
return c.get('password').value === c.get(' confirm_password').value
this.myForm = this.fb.group({
name: ['', Validators.compose([Validators.required])],
email: [''],
passwordgroup: this.fb.group({
password: ['', Validators.compose([Validators.required])],
confirm_password: ['', Validators.compose([])]
}, { validator: passwordConfirming })
});
this.myForm = this.fb.group({
name: ['', Validators.compose([Validators.required])],
email: [''],
passwordgroup: this.fb.group({
password: ['', Validators.compose([Validators.required])],
confirm_password: ['', Validators.compose([])]
}, passwordConfirming)
});
删除上面的 'validator' 键(取消此对象表示法)并仅使用验证器名称 'passwordConfirming' 没有语法错误。
我们如何使用angular2验证来确认密码?我正在使用 angular 2.0 final。
我正在使用 formbuilder class 用于模型驱动的表单,如下所示:
this.myForm = this.fb.group({
name: ['', Validators.compose([Validators.required])],
email: [''],
passwordgroup: this.fb.group({
password: ['', Validators.compose([Validators.required])],
confirm_password: ['', Validators.compose([])]
})
});
您可以像这样将此函数添加到您的组件中,从而简单地使用自定义验证器
import { AbstractControl } from '@angular/forms';
function passwordConfirming(c: AbstractControl) {
return c.get('password').value === c.get(' confirm_password').value
this.myForm = this.fb.group({
name: ['', Validators.compose([Validators.required])],
email: [''],
passwordgroup: this.fb.group({
password: ['', Validators.compose([Validators.required])],
confirm_password: ['', Validators.compose([])]
}, { validator: passwordConfirming })
});
this.myForm = this.fb.group({
name: ['', Validators.compose([Validators.required])],
email: [''],
passwordgroup: this.fb.group({
password: ['', Validators.compose([Validators.required])],
confirm_password: ['', Validators.compose([])]
}, passwordConfirming)
});
删除上面的 'validator' 键(取消此对象表示法)并仅使用验证器名称 'passwordConfirming' 没有语法错误。