我是 Angular 2 的新手,我想为表单控件实现一个自定义验证器,它以布尔值(requiredAttribute)作为参数。
如果此参数为真,则需要表单控件,否则不需要。
我已经实现了这个,但似乎不起作用。需要所有输入(表单控制)。我已经实现了这个代表自定义验证器的函数。
function inputRequired( requiredAttribute: boolean) {
return (control: FormControl): { [s: string]: boolean } => {
if (requiredAttribute === false) {
return {'input is not required': true};
}else {
return null;
}
};
}
我已经把它放在了 initForm 方法中。然后对于我的反应式表单的输入表单文本:
text: new FormControl('', [Validators.compose([inputRequired(this.selectedOperation.inputs[i].required)])]),
最终代码
private initForm() {
function inputRequired( requiredAttribute: boolean) {
return (control: FormControl): { [s: string]: boolean } => {
if (requiredAttribute === false) {
return {'input is not required': true};
}else {
return null;
}
};
}
let operationName: any;
const operationInputs: FormArray = new FormArray([]);
if (this.selectedOperation.inputs != null) {
for (let i = 0; i < this.selectedOperation.inputs.length; i++ ) {
operationInputs.push(
new FormGroup({
name: new FormControl(this.selectedOperation.inputs[i].name),
text: new FormControl('', [Validators.compose([inputRequired(this.selectedOperation.inputs[i].required)])]),
defaultText: new FormControl(this.selectedOperation.inputs[i].defaultText),
complexType: new FormControl(this.selectedOperation.inputs[i].complexType),
type: new FormControl(this.selectedOperation.inputs[i].type),
isMultivalued: new FormControl(this.selectedOperation.inputs[i].isMultiValued),
values: new FormControl(this.selectedOperation.inputs[i].values),
indicator: new FormControl(this.selectedOperation.inputs[i].indicator),
required: new FormControl(this.selectedOperation.inputs[i].required),
isSelected: new FormControl(this.selectedOperation.inputs[i].isSelected),
simpleTypeVarietyOrComplexTypeContent: new FormControl(this.selectedOperation.inputs[i].simpleTypeVarietyOrComplexTypeContent),
choiceContent: new FormControl(this.selectedOperation.inputs[i].choiceContent),
inputQname: new FormControl(this.selectedOperation.inputs[i].inputQname),
attributes: new FormControl(this.selectedOperation.inputs[i].attributes),
children: operationInputsChildren,
})
);
}
}
operationName = this.selectedOperation.name;
this.operationRequestForm = this.formBuilder.group({
wsdlPath: [this.wsdlPath],
name: [operationName],
inputs: operationInputs,
operationDateInvoke: ['', Validators.required],
operationTimeInvoke: ['', Validators.required]
});
}
并且输入是 CustomInput 类的对象,该对象具有必需的属性。
export class CustomInput {
constructor(public name: string, public text: string, public
defaultText: string,
public complexType: boolean, public type: string, public children:
CustomInput[] = [],
public isMultiValued: boolean,
public values: string[] = [], public indicator: string, public
required: boolean,
public isSelected: boolean, public
simpleTypeVarietyOrComplexTypeContent: number,
public choiceContent: boolean, public inputQname: string,
public attributes: Map<string, string> = new Map<string, string>()
) {}
}
一个操作有许多输入元素。我想为操作创建一个反应形式。如果输入元素是必需的(其属性要求 eqaul 为 true),则需要与操作输入元素关联的 HTML 输入。
所以我如何实现一个带有布尔参数的自定义验证器,如果这个参数为真,那么表单控件是必需的,否则不是。
谢谢