唯一值 angular 反应形式
unique value angular reactive forms
我对 Angular 4 种反应形式有疑问。
我的申请是一个培训项目。它在 SpringBoot 中有一个后端,mySql 数据库,在 Angular4.
中有表单
此应用程序必须将医生添加到数据库,但也必须验证:许可证号必须是唯一的。我正在使用响应式表单(不是模板驱动的)。
我尝试用函数解决我的问题:
import { AbstractControl } from "@angular/forms";
export function ControlLicenseNumber(c: AbstractControl): string | null {
const formGroup = c.parent.controls;
return Object.keys(formGroup).find(license_number => c === formGroup[license_number]) || null;
}
但我不知道如何将它注入我的 formControl:
constructor(private doctorsService: DoctorsService,
private location: Location,
private fb: FormBuilder) {
this.rForm = new FormGroup({
'name': new FormControl('', Validators.required),
'surname': new FormControl('', Validators.required),
'phone': new FormControl('', Validators.required),
'nationality': new FormControl('', Validators.required),
'email': new FormControl('', Validators.email),
'license_number': new FormControl('', [Validators.required, ControlLicenseNumber])})
}
希望你们能帮助我。问候
像这样尝试:-
constructor(private doctorsService: DoctorsService,
private location: Location,
private fb: FormBuilder) {
this.rForm = this.fb.group({({
name : ['', [Validators.required]],
surname : ['', [Validators.required]],
phone : ['', [Validators.required],
nationality : ['',[Validators.required]],
email : ['', Validators.email],
license_number : ['', [Validators.required]
},
{
validator: this.ControlLicenseNumber
});
}
ControlLicenseNumber(c: AbstractControl){
const value = c.get('license_number').value;
return Object.keys(value).find(license_number => c ===
value) || null;
}
我对 Angular 4 种反应形式有疑问。
我的申请是一个培训项目。它在 SpringBoot 中有一个后端,mySql 数据库,在 Angular4.
中有表单此应用程序必须将医生添加到数据库,但也必须验证:许可证号必须是唯一的。我正在使用响应式表单(不是模板驱动的)。
我尝试用函数解决我的问题:
import { AbstractControl } from "@angular/forms";
export function ControlLicenseNumber(c: AbstractControl): string | null {
const formGroup = c.parent.controls;
return Object.keys(formGroup).find(license_number => c === formGroup[license_number]) || null;
}
但我不知道如何将它注入我的 formControl:
constructor(private doctorsService: DoctorsService,
private location: Location,
private fb: FormBuilder) {
this.rForm = new FormGroup({
'name': new FormControl('', Validators.required),
'surname': new FormControl('', Validators.required),
'phone': new FormControl('', Validators.required),
'nationality': new FormControl('', Validators.required),
'email': new FormControl('', Validators.email),
'license_number': new FormControl('', [Validators.required, ControlLicenseNumber])})
}
希望你们能帮助我。问候
像这样尝试:-
constructor(private doctorsService: DoctorsService,
private location: Location,
private fb: FormBuilder) {
this.rForm = this.fb.group({({
name : ['', [Validators.required]],
surname : ['', [Validators.required]],
phone : ['', [Validators.required],
nationality : ['',[Validators.required]],
email : ['', Validators.email],
license_number : ['', [Validators.required]
},
{
validator: this.ControlLicenseNumber
});
}
ControlLicenseNumber(c: AbstractControl){
const value = c.get('license_number').value;
return Object.keys(value).find(license_number => c ===
value) || null;
}