Angular 4 Model Driven表单组验证,密码匹配无效

时间:2017-06-21 14:38:49

标签: angular typescript angular2-forms angular-validation angular4-forms

我使用Angular 4.0.0进行自定义组验证时遇到问题 此表单组始终显示null {{modelform.get(' passwords')。errors | json}}

组件

import { Component, OnInit } from '@angular/core';
import { AbstractControl, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';

@Component({
    templateUrl: './mdf-costomvalidation.component.html'
})
export class MDFCustomValidationComponent implements OnInit {

    modelform: FormGroup;
    constructor(private _formBuilder: FormBuilder) { }

    ngOnInit() {
        this.modelform = this._formBuilder.group({
            firstname: ['', Validators.required],
            lastname: ['', Validators.required],
            passwords: this._formBuilder.group({
                firstpass: ['', Validators.required],
                secondpass: ['', Validators.required]
            }, { Validator: this.passwordMatch })
        });
    }
    passwordMatch(c: AbstractControl) {
        return c.get('firstpass').value === c.get('secondpass').value ? null : {'nomatch': true};
    }
    modelformsubmit() {
            console.log(this.modelform.value);
    }
}

我试图在类之外声明passwordMatch()函数,并提到第二个参数为' passwordMatch'没有'这个'关键字根据Angular.io API FormGroup示例,但对返回值没有影响。

function passwordMatch(c: AbstractControl) {
    return c.get('firstpass').value === c.get('secondpass').value ? null : {'nomatch': true};
}

passwords: this._formBuilder.group({
    firstpass: ['', Validators.required],
    secondpass: ['', Validators.required]
}, passwordMatch)

0 个答案:

没有答案