我正在做一个项目,我想在表单数组字段上进行所需的验证。这是我的模板
<div formArrayName="price" *ngFor="let p of vehicleForm.get('prices.price').controls; let i=index">
<div class="price-form-repeater" [formGroupName]="i">
<div class="col-md-6 col-sm-12 cust-col">
<label class="col-sm-4 dark-label" for="priceType">Price Type:</label>
<div class="col-md-6" >
<select id="priceType" formControlName="type" class="form-control" data-placeholder="select" >
<option ></option><!-- Required for data-placeholder attribute to work with Chosen plugin -->
<option value="public">Public</option>
</select>
<div *ngIf="p.controls.type.errors && (p.controls['type'].dirty || p.controls['type'].touched)) ||
(p.controls['type'].untouched && formSubmitAttempt)"class="alert alert-danger">
<div [hidden]="(!p.controls['type'].errors.required)">Price type is required.</div>
</div>
</div>
</div>
</div>
</div>
这是我的组件
prices: this.fb.group({
price: this.fb.array([this.priceField()]) ,
})
priceField():FormGroup {
return this.fb.group({
type: ['',Validators.required],
vat_type: ['',Validators.required],
currency: ['',Validators.required],
negotiable: ['',Validators.required],
value: ['',Validators.required],
tax_deduction: ['',Validators.required]
});
}
如果用户试图将这些字段留空,我只想显示必填字段错误消息。我感谢你的帮助。