我一直试图在Ionic/Angular2中使用模型表单。我有一个表单,我想要做一些有条件的验证。用户从6个需要的字段开始,然后可以选择“手动”和“自动”处理。如果选择“自动”,则会渲染3个表单输入/选择。我希望这3个字段从不需要的地方变成必需的。尝试使用Angular2表单模型执行条件验证器。尝试使用myForm.setValidators(),但似乎不起作用
这里的模型开始:
this.myForm = this.formBuilder.group({
name: ['', Validators.compose([Validators.required, Validators.maxLength(45), Validators.minLength(2)])],
img: [''],
longDescription: ['', Validators.compose([Validators.required, Validators.maxLength(200)])],
shortDescription: ['', Validators.compose([Validators.required, Validators.maxLength(45)])],
processingType: [this.PROCESSING_TYPE.MANUAL],
discountType: ['' ],
discountRule: ['' ],
discountAmount: ['' ], // money, isNumbersOnly handled by custom validator
product: [''],
dateRuleDays: ['' ],
dateRuleTimeStart: ['' ],
dateRuleTimeEnd: ['' ],
startDate: ['', Validators.required],
expiryDate: ['', Validators.required ]
});
discountType,discountRule,discountAmount是3场,我想切换到被要求。
这是我已经试过:
标记,其中setProcessingType FN被调用(如果需要的话我可以提供完整的页面),当用户点击“手动”之间
<!-- automatic or manual -->
<h4 margin-top text-center>Processing Type</h4>
<ion-row text-center>
<ion-col width-50>
<button class="width-80" color="primary" (click)="setProcessingType(PROCESSING_TYPE.MANUAL)" ion-button margin-top>{{ PROCESSING_TYPE.MANUAL }}</button>
</ion-col>
<ion-col width-50>
<button class="width-80" color="primary" (click)="setProcessingType(PROCESSING_TYPE.AUTOMATIC)" ion-button margin-top>{{ PROCESSING_TYPE.AUTOMATIC }}</button>
</ion-col>
</ion-row>
setProcessingType被称为'自动'按钮。键入:string是我将设置为processingType的新值。
setProcessingType(type: string): void {
this.myForm.patchValue({ // i welcome comments to correct this to 'setValue()' if needed
processingType: type
});
let formCtrls = ['discountType', 'discountRule', 'discountAmount', 'dateRuleDays', 'dateRuleTimeStart', 'dateRuleTimeEnd'];
let add = [Validators.required];
let empty = [];
formCtrls.forEach((key, index) => {
if (this.myForm.controls && this.myForm.controls[key]) {
if (type === this.PROCESSING_TYPE.AUTOMATIC) {
this.myForm.controls[key].setValidators([Validators.required]);
} else {
console.log('resetting validators...');
this.myForm.controls[key].setValidators([]);
console.log('errors on : ', key, this.myForm.controls[key].errors);
// this.myForm.controls[key].markAsUntouched(); // attempt 3928
// this.myForm.controls[key].markAsPristine() // attempt 3929
//this.myForm.controls.updateValueAndValidty(); // this throws an error
}
}
})
console.log('new form controls: ', this.myForm.controls);
}
我还订阅了“processingType” valueChanges这里我跑上面
this.myForm.get('processingType').valueChanges.subscribe(data => this.onProcessingTypeChanged(data));
在这里,相同的逻辑功能是从我的值的日志一些截图我试图添加一个验证程序到:
开始(它应该是怎样的)...
基于上述怀疑,我怀疑
this.myForm.controls.updateValueAndValidty();
或 this.myForm.controls [键] .updateValueAndValidity();
是问题,但正如我在代码中所评论的那样,这两者都抛出了“不是函数”的错误。
我已经搜索了几乎所有的条件验证组合,angular2表单模型,动态添加校验器等......太多的链接到这里列出,哈哈。
希望这足以让某人帮助诊断问题。
在此先感谢。