我有以下代码:
<md2-dialog #techniciansDialog>
<md2-dialog-title color="primary">Técnicos</md2-dialog-title>
<form #technicianForm="ngForm">
<div style="display: table; width: 100%;">
<div style="display: table; width: 100%;">
<div style="vertical-align:middle; width:50%; display: table-cell;">
<md-input-container>
<input mdInput [(ngModel)]="technician.name" name="nameTechnician" type="text" placeholder="Nome" required>
</md-input-container>
</div>
</div>
<div style="vertical-align:middle; width:50%; display: table-cell;">
<md-input-container>
<input mdInput [(ngModel)]="technician.responsability" name="responsabilityTechnician" type="text"
placeholder="Responsabilidade" required>
</md-input-container>
</div>
</div>
<div style="display: table; width: 100%;">
<div style="vertical-align:middle; width:50%; display: table-cell;">
<md-input-container>
<input mdInput [(ngModel)]="technician.phone" name="phoneTechnician" type="text" placeholder="Telefone"
required>
</md-input-container>
</div>
<md-input-container>
<input mdInput [(ngModel)]="technician.email" name="emailTechnician" type="text" placeholder="Email" required>
</md-input-container>
<md-input-container>
<input mdInput [(ngModel)]="technician.password" name="passwordTechnician" type="password"
placeholder="Password" required>
</md-input-container>
</div>
</form>
<md2-dialog-footer>
<div *ngIf="!update;then content else other_content"></div>
<ng-template #content>
<button md-raised-button color="primary" [disabled]="!technicianForm.form.valid"
(click)="gravarDadosTechnician(); technicianForm.reset()">Criar
</button>
</ng-template>
<ng-template #other_content>
<button md-raised-button color="primary" [disabled]="!technicianForm.form.valid"
(click)="sendUpdateDadosTechnician(techniciansDialog); technicianForm.reset()">Atualizar
</button>
</ng-template>
<button md-raised-button color="primary" (click)="closeDialog(techniciansDialog); technicianForm.reset()">Fechar
</button>
</md2-dialog-footer>
</md2-dialog>
<md2-dialog #serviceDialog>
<md2-dialog-title color="primary">Serviços</md2-dialog-title>
<form #servicesForm="ngForm" name="servicesForm">
<div style="display: table; width: 100%;">
<div *ngIf="!update;then divcreate else divupdate"></div>
<div style="vertical-align:middle; width:50%; display: table-cell;">
<md-input-container>
<input mdInput [(ngModel)]="service.name" name="nameService" type="text" placeholder="Nome" required>
</md-input-container>
</div>
</div>
<div style="display: table; width: 100%;">
<div style="vertical-align:middle; width:50%; display: table-cell;">
<md-input-container>
<input mdInput [(ngModel)]="service.SLA" name="SLA" type="text" placeholder="SLA (HORAS)" required>
</md-input-container>
</div>
<div style="vertical-align:middle; width:50%; display: table-cell;">
<md-input-container>
<input mdInput [(ngModel)]="service.description" name="descriptionService" type="text"
placeholder="description"
required>
</md-input-container>
</div>
</div>
</form>
<md2-dialog-footer>
<div *ngIf="!update;then content else other_content"></div>
<ng-template #content>
<button md-raised-button color="primary" [disabled]="!servicesForm.form.valid"
(click)="gravarDadosServices(); servicesForm.reset()">Criar
</button>
</ng-template>
<ng-template #other_content>
<button md-raised-button color="primary" [disabled]="!servicesForm.form.valid"
(click)="sendUpdateDadosServices(serviceDialog); servicesForm.reset()">Atualizar
</button>
</ng-template>
<button md-raised-button color="primary" (click)="closeDialog(serviceDialog); servicesForm.reset()">Fechar</button>
</md2-dialog-footer>
</md2-dialog>
当我不验证它们时,或者我只是验证其中一个时,两种形式都能正常工作。
例如:
servicesForm
可以正常使用验证,但是当我填写technicianForm
时,即使我正确填写字段,它也不会验证。
technicianForm
只是没有回答,它仍然是technicianForm.form.valid
因此,如果我取消#servicesForm
,#technicianForm
就会完美无缺。
如何验证这些多个表单字段,因为我将在同一页面上有更多2个表单。
我是否必须在我的.ts文件中为每个和其中一个进行表单验证?
答案 0 :(得分:10)
所以我发布了一个答案,所以我们可以关闭这个问题。有几种选择。
1)您可以为每个表单创建一个单独的组件,并将这些组件嵌套在包含所需表单集的父组件中。这提供了良好的关注点分离,并使每个组件保持较小。
2)如果多个表单的目的是用于分组(而不是单独提交),则可以使用FormGroup跟踪一组相关的控件。但这听起来不像这样。
您还可以在此处查看卡拉的视频,了解更多选项和讨论:https://www.youtube.com/watch?v=MfILq1LNSUk
答案 1 :(得分:1)
如果检查表单或字段是否未定义如下,您还可以显式检查表单错误/字段错误:
[disabled]="technicianForm && technicianForm.form.invalid"
或者,如果您有一个必填字段,例如,并且想要显示错误(输入字段为:#name =“ngModel”):
<div class="col-md-12 text-danger" *ngIf="name && name.errors && name.touched">
{{ '_MY_NAME_ERROR_' | translate }}
</div>
这对我来说有多种形式(像#myform =“ngForm”这样构建)在一个带有一个组件的模板中。