获取表单控件的名称
Get name of Form Control
我在 angular 中使用反应式表单,我需要比较开始日期 "start date" 和结束日期 "end date",两个控件都在 [=18] 中验证=] 函数,但问题是我不知道如何求控制正在求值
//Some stuff
public fechaInicio = new FormControl('', [
Validators.required
, this.dateLessThanTo
]);
public fechaFin = new FormControl('', [
Validators.required
, this.dateLessThan
]);
createForm() {
this.contratoForm = this.formBuilder.group({
fechas: this.formBuilder.group({
fechaInicio: this.fechaInicio,
fechaFin: this.fechaFin
}, { validator: this.dateLessThan('fechaInicio', 'fechaFin') }),
});
}
这里我需要知道比较日期的控件名称:
dateLessThanTo(fieldControl: FormControl) {
//
//if (fechaInicio.value > FechaFin.value){
// return true;
//}
//else{
// return false;
// }
}
//Some stuff
你只需要这样做,你可以检查一个表单中的个体 FormControl
通过使用 .get() 方法提取它。
contratoForm.get('fechaInicio').value
dateLessThanTo(fieldControl: FormControl) {
let va= fieldControl.get('fechaInicio').value ;
let va1 = fieldControl.get('FechaFin').value ;
}
在此处查看:https://angular.io/guide/reactive-forms#inspect-formcontrol-properties
你可以这样做:
Object.getOwnPropertyNames(this.contratoForm['controls']['fechas']['controls']).map((key: string) => {
// Something like this. Not sure how your form looks like
在您的自定义验证器中,您得到 formGroup
fechas
,因此您不需要从 TS 代码传递任何参数:
createForm() {
this.contratoForm = this.formBuilder.group({
fechas: this.formBuilder.group({
fechaInicio: this.fechaInicio,
fechaFin: this.fechaFin
}, { validator: this.dateLessThanTo }),
});
}
并在您的自定义验证器中:
dateLessThanTo(group: FormGroup) {
if (group.controls.fechaInicio.value > group.controls.fechaFin.value){
return {notValid: true}
}
return null;
}
有效时需要return null
,无效时设置错误,例如notValid
。
从控件中获取父组,然后与当前控件进行比较:
dateLessThanTo(control: AbstractControl) {
let name = this.getName(control);
...
}
private getName(control: AbstractControl): string | null {
let group = <FormGroup>control.parent;
if (!group) {
return null;
}
let name: string;
Object.keys(group.controls).forEach(key => {
let childControl = group.get(key);
if (childControl !== control) {
return;
}
name = key;
});
return name;
}
在您的组件中,您可以添加一个像这样的自定义验证器
static customValidator(control: AbstractControl): { [key: string]: any } {
const controlName = (Object.keys(control.parent.controls).find(key => control.parent.controls[key] === control));
if (control.value === 0) {
return {key: {error: 'invalid'}};
}
return null; }
在 controlName 中,您将拥有控件的名称。
我在 angular 中使用反应式表单,我需要比较开始日期 "start date" 和结束日期 "end date",两个控件都在 [=18] 中验证=] 函数,但问题是我不知道如何求控制正在求值
//Some stuff
public fechaInicio = new FormControl('', [
Validators.required
, this.dateLessThanTo
]);
public fechaFin = new FormControl('', [
Validators.required
, this.dateLessThan
]);
createForm() {
this.contratoForm = this.formBuilder.group({
fechas: this.formBuilder.group({
fechaInicio: this.fechaInicio,
fechaFin: this.fechaFin
}, { validator: this.dateLessThan('fechaInicio', 'fechaFin') }),
});
}
这里我需要知道比较日期的控件名称:
dateLessThanTo(fieldControl: FormControl) {
//
//if (fechaInicio.value > FechaFin.value){
// return true;
//}
//else{
// return false;
// }
}
//Some stuff
你只需要这样做,你可以检查一个表单中的个体 FormControl
通过使用 .get() 方法提取它。
contratoForm.get('fechaInicio').value
dateLessThanTo(fieldControl: FormControl) {
let va= fieldControl.get('fechaInicio').value ;
let va1 = fieldControl.get('FechaFin').value ;
}
在此处查看:https://angular.io/guide/reactive-forms#inspect-formcontrol-properties
你可以这样做:
Object.getOwnPropertyNames(this.contratoForm['controls']['fechas']['controls']).map((key: string) => {
// Something like this. Not sure how your form looks like
在您的自定义验证器中,您得到 formGroup
fechas
,因此您不需要从 TS 代码传递任何参数:
createForm() {
this.contratoForm = this.formBuilder.group({
fechas: this.formBuilder.group({
fechaInicio: this.fechaInicio,
fechaFin: this.fechaFin
}, { validator: this.dateLessThanTo }),
});
}
并在您的自定义验证器中:
dateLessThanTo(group: FormGroup) {
if (group.controls.fechaInicio.value > group.controls.fechaFin.value){
return {notValid: true}
}
return null;
}
有效时需要return null
,无效时设置错误,例如notValid
。
从控件中获取父组,然后与当前控件进行比较:
dateLessThanTo(control: AbstractControl) {
let name = this.getName(control);
...
}
private getName(control: AbstractControl): string | null {
let group = <FormGroup>control.parent;
if (!group) {
return null;
}
let name: string;
Object.keys(group.controls).forEach(key => {
let childControl = group.get(key);
if (childControl !== control) {
return;
}
name = key;
});
return name;
}
在您的组件中,您可以添加一个像这样的自定义验证器
static customValidator(control: AbstractControl): { [key: string]: any } {
const controlName = (Object.keys(control.parent.controls).find(key => control.parent.controls[key] === control));
if (control.value === 0) {
return {key: {error: 'invalid'}};
}
return null; }
在 controlName 中,您将拥有控件的名称。