我是新的角2,我想实现一个自定义验证器的窗体控件,采用布尔(requiredAttribute)作为参数。重写自定义验证器角2
如果此参数为true,则表单控件是必需的,否则它不是必需的。
我已经实现了这一点,但似乎没有奏效。所有输入(表格控制)becoms需要。 我已经实现了代表自定义验证器的这个函数。
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输入。
因此,我如何实现一个采用布尔参数的自定义验证器,如果此参数为true,则表单控件是必需的,否则不是。
感谢
感谢您的答复,但它没”工作。即使我输入html elemnt输入的所有值,表单也会被禁用。 – adem
你好,okey谢谢 – adem
你能否创建一个可以工作的plunker,我很乐意看看你的代码:) – Alex