[英]Input field required only if radio button is checked - Angular 4
I have a reactive form with 3 radio buttons and their corresponding inputs text (so 3 too). 我有一个反应式表单,带有3个单选按钮及其相应的输入文本(也是3个)。 If i click on a radio button, i display just its corresponding inputs text.
如果单击单选按钮,则仅显示其相应的输入文本。 I want to make the input text required only if I click on its radio button.
仅在单击单选按钮时,才需要输入文本。 I use a custom validator in addition to required, so i can't use the html way.
除了必需之外,我还使用自定义验证器,因此我无法使用html方式。
My form is initialized like this in my component.ts : 我的表单在我的component.ts中初始化为:
initializeForm() {
this.formGroupItemSelection = this.fb.group({
radioBoutton:'refNumber',
refNumberSelected:[
null,
Validators.compose([Validators.required, matchValuesRefNumber(this.listOfItems)])
],
partNumberSelected:[
null,
Validators.compose([Validators.required, matchValuesPartNumber(this.listOfItems)])
],
itemNameSelected: [
null,
Validators.compose([Validators.required, matchValuesItemName(this.listOfItems)])
]
})
}
How can i make inputs text required dynamically and not at initialization ? 我该如何动态而不是在初始化时要求输入文本?
Validator.ts (example with matchValuesRefNumber ) : Validator.ts (具有matchValuesRefNumber的示例):
export const matchValuesRefNumber = (valuesToCheck: any[]): ValidatorFn => {
return (control: AbstractControl): { [key: string]: boolean } => {
const controlValue = control.value;
let res = valuesToCheck.findIndex(el => el.refNumber.input === controlValue);
console.log(res);
return res !== -1 ? null : { findmatch: true };
};
};
component.html component.html
<section class="CreateItem" *ngIf="formGroupItemSelection">
<form (ngSubmit)="addItem()" [formGroup]="formGroupItemSelection">
<input formControlName="radioBoutton" type="radio" value="refNumber" checked> ref number
<br>
<input formControlName="radioBoutton" type="radio" value="partNumber"> part number
<br>
<input formControlName="radioBoutton" type="radio" value="itemName"> item name
<br/>
<br>
<div *ngIf="formGroupItemSelection.controls.radioBoutton.value==='refNumber'">
<input list="refNumbers" formControlName="refNumberSelected" type="text" name="refNumberSelected">
<datalist id="refNumbers">
<option *ngFor="let ref of listOfItems">{{ref.refNumber.input}}</option>
</datalist>
</div>
<div *ngIf="formGroupItemSelection.controls.radioBoutton.value==='partNumber'">
<input list="partNumbers" formControlName="partNumberSelected" type="text" name="partNumberSelected">
<datalist id="partNumbers">
<option *ngFor="let ref of listOfItems">{{ref.partNumber.input}}</option>
</datalist>
</div>
<div *ngIf="formGroupItemSelection.controls.radioBoutton.value==='itemName'">
<input list="itemsName" formControlName="itemNameSelected" type="text" name="itemNameSelected">
<datalist id="itemsName">
<option *ngFor="let ref of listOfItems">{{ref.itemDesignation.input}}</option>
</datalist>
</div>
<button type="submit" [disabled]="!formGroupItemSelection.valid">Valider</button>
</form>
</section>
I had something similar: 我有类似的东西:
Here I only wanted the Phone to be a required field if the Send Notifications
radio button was set to text
. 在这里,我仅希望“
Send Notifications
单选按钮设置为text
,“电话”为必填字段。 If it was set to email
, the phone was not required. 如果将其设置为
email
,则不需要电话。
My code looks like this: 我的代码如下所示:
In the ngOnInit right after I build the form: 构建表单后,立即在ngOnInit中:
this.customerForm.get('notification').valueChanges
.subscribe(value => this.setNotification(value));
And later in the same file: 然后在同一个文件中:
setNotification(notifyVia: string): void {
const phoneControl = this.customerForm.get('phone');
if (notifyVia === 'text') {
phoneControl.setValidators(Validators.required);
} else {
phoneControl.clearValidators();
}
phoneControl.updateValueAndValidity();
}
You can find the complete source code here: https://github.com/DeborahK/Angular2-ReactiveForms/tree/master/Demo-Final-Updated 您可以在此处找到完整的源代码: https : //github.com/DeborahK/Angular2-ReactiveForms/tree/master/Demo-Final-Updated
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.