您如何验证基于 Angular2 中另一个控件的 FormBuilder 控件?
How do you validate a FormBuilder control based on another in Angular2?
我在 Angular2 中使用 FormBuilder 并创建了自定义验证器服务。
组件
this.exampleForm = this.formBuilder.group({
'FirstName': ['', Validators.required],
'LastName': ['', [Validators.required]],
'Description': [''],
'Location': [''],
});
this.exampleForm .addControl("StartDate", this.formBuilder.group({
'Month': [0, [Validators.required, ValidationService.monthValidator]],
'Year': [0, [Validators.required, ValidationService.yearValidator]]
}));
this.exampleForm .addControl("EndDate", this.formBuilder.group({
'Month': [0],
'Year': [0]
}));
HTML分量
<div formGroupName="StartDate">
<select formControlName="Month">
<option>...</option>
</option>
</select>
<select formControlName="Year">
<option>...</option>
</select>
</div>
我不想让 EndDate 早于 StartDate
如何验证基于 StarDate 控件的 EndDate 控件?
编辑
试过这样的事情:
this.exampleForm = this.formBuilder.group({
...
}, passwordMatchValidator);
function passwordMatchValidator(g: FormGroup) {
return g.get('FirstName').value === g.get('LastName').value
? null : {'mismatch': true};
}
它什么也没做
您应该将您的两个控件分组到一个 FormGroup 中并测试此 FormGroup 是否有效
https://angular.io/docs/ts/latest/api/forms/index/FormGroup-class.html
编辑
默认情况下,如果每个 FormControl 都有效,则表单组有效。
您可以像为 FormControl 一样为 FormGroup 定义自定义验证器。
在此示例中,我向包含两个 FormControl 的 FormGroup 添加了一个验证器 (passwordMatchValidator)。
Whosebug-form.component.ts
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, FormBuilder } from '@angular/forms';
@Component({
selector: 'Whosebug-form',
templateUrl: 'Whosebug-form.component.html',
styleUrls: [
'Whosebug-form.component.css'
],
providers: []
})
export class WhosebugFormComponent implements OnInit {
public myForm: FormGroup;
public alphaCtrl: FormControl;
public betaCtrl: FormControl;
constructor(fb: FormBuilder) {
this.alphaCtrl = fb.control('alphaValue', [WhosebugFormComponent.myControlValidator]);
this.betaCtrl = fb.control('betaValue', [WhosebugFormComponent.myControlValidator]);
this.myForm = new FormGroup({
alpha: this.alphaCtrl,
beta: this.betaCtrl,
}, WhosebugFormComponent.passwordMatchValidator);
}
// Custom FormControl Validator
static myControlValidator(control: FormControl) {
return control.value.length > 1 ? null : { myInvalid: true};
}
// Custom FormGroup Validator
static passwordMatchValidator(g: FormGroup) {
return g.get('alpha').value === g.get('beta').value
? null : {'mismatch': true};
}
ngOnInit() {}
}
Whosebug-form.component.html
<form (ngSubmit)="onSubmit()" [formGroup]="myForm">
<input formControlName="alpha" name="alpha"/>
<input formControlName="beta" name="beta"/>
<button type="submit" [disabled]="!myForm.valid">SEND</button>
</form>
Whosebug-form.component.css
input.ng-invalid {
background-color: red;
}
input.ng-valid {
background-color: greenyellow;
}
我在 Angular2 中使用 FormBuilder 并创建了自定义验证器服务。
组件
this.exampleForm = this.formBuilder.group({
'FirstName': ['', Validators.required],
'LastName': ['', [Validators.required]],
'Description': [''],
'Location': [''],
});
this.exampleForm .addControl("StartDate", this.formBuilder.group({
'Month': [0, [Validators.required, ValidationService.monthValidator]],
'Year': [0, [Validators.required, ValidationService.yearValidator]]
}));
this.exampleForm .addControl("EndDate", this.formBuilder.group({
'Month': [0],
'Year': [0]
}));
HTML分量
<div formGroupName="StartDate">
<select formControlName="Month">
<option>...</option>
</option>
</select>
<select formControlName="Year">
<option>...</option>
</select>
</div>
我不想让 EndDate 早于 StartDate
如何验证基于 StarDate 控件的 EndDate 控件?
编辑
试过这样的事情:
this.exampleForm = this.formBuilder.group({
...
}, passwordMatchValidator);
function passwordMatchValidator(g: FormGroup) {
return g.get('FirstName').value === g.get('LastName').value
? null : {'mismatch': true};
}
它什么也没做
您应该将您的两个控件分组到一个 FormGroup 中并测试此 FormGroup 是否有效 https://angular.io/docs/ts/latest/api/forms/index/FormGroup-class.html
编辑
默认情况下,如果每个 FormControl 都有效,则表单组有效。 您可以像为 FormControl 一样为 FormGroup 定义自定义验证器。 在此示例中,我向包含两个 FormControl 的 FormGroup 添加了一个验证器 (passwordMatchValidator)。
Whosebug-form.component.ts
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, FormBuilder } from '@angular/forms';
@Component({
selector: 'Whosebug-form',
templateUrl: 'Whosebug-form.component.html',
styleUrls: [
'Whosebug-form.component.css'
],
providers: []
})
export class WhosebugFormComponent implements OnInit {
public myForm: FormGroup;
public alphaCtrl: FormControl;
public betaCtrl: FormControl;
constructor(fb: FormBuilder) {
this.alphaCtrl = fb.control('alphaValue', [WhosebugFormComponent.myControlValidator]);
this.betaCtrl = fb.control('betaValue', [WhosebugFormComponent.myControlValidator]);
this.myForm = new FormGroup({
alpha: this.alphaCtrl,
beta: this.betaCtrl,
}, WhosebugFormComponent.passwordMatchValidator);
}
// Custom FormControl Validator
static myControlValidator(control: FormControl) {
return control.value.length > 1 ? null : { myInvalid: true};
}
// Custom FormGroup Validator
static passwordMatchValidator(g: FormGroup) {
return g.get('alpha').value === g.get('beta').value
? null : {'mismatch': true};
}
ngOnInit() {}
}
Whosebug-form.component.html
<form (ngSubmit)="onSubmit()" [formGroup]="myForm">
<input formControlName="alpha" name="alpha"/>
<input formControlName="beta" name="beta"/>
<button type="submit" [disabled]="!myForm.valid">SEND</button>
</form>
Whosebug-form.component.css
input.ng-invalid {
background-color: red;
}
input.ng-valid {
background-color: greenyellow;
}