简体   繁体   English

Angular2-触发表单验证

[英]Angular2 - Trigger form validation

I have a form with 5 polymer elements. 我有一个包含5个聚合物元素的表格。 When i click the save button outside the form, in case any of the fields doenst have any valid data in them, i want to trigger the validations and display the error message for each field that doesnt meet the expected result. 当我单击表单外部的保存按钮时,如果任何字段中确实有任何有效数据,我想触发验证并为每个不符合预期结果的字段显示错误消息。

I have tried this but doesnt work: 我已经尝试过了,但是没有用:

 saveGroupDetails() {
    for(let i=0;i<Object.keys(this.groupDetailsForm.controls).length;i++){
      let key = Object.keys(this.groupDetailsForm.controls)[i];
       this.groupDetailsForm.controls[key].markAsTouched();
       this.groupDetailsForm.controls[key].markAsDirty();
       this.groupDetailsForm.controls[key].updateValueAndValidity();
    }
    this.cdr.detectChanges();

The form is: 形式是:

<form #groupDetailsFormVar="ngForm" name="groupDetailsForm">
                    <paper-input  auto-validate ="true" name="code" [readonlyCustomGroups]="groupRights===false" class="paddingInput" 
                                  error-message="Field required!" required="true" label="Code" [disabled]="fromEdit" [(ngModel)]="obj.code" ngDefaultControl></paper-input>
                    <paper-input  auto-validate ="true" name="name" [readonlyCustomGroups]="groupRights===false" class="paddingInput" 
                                  error-message="Field required!" required="true" label="Name" [(ngModel)]="obj.name" ngDefaultControl ></paper-input>
                    <vaadin-combo-box   auto-validate ="true" name="region" [readonlyCustomGroups]="groupRights===false"
                                        error-message="Field required!" required [items]="REGION"
                                        class="paddingInput"
                                        item-label-path="name"
                                        item-value-path="code"
                                        [value]="obj.regionCode"
                                        [(ngModel)]="obj.region"
                                        label="Region" 
                                        ngDefaultControl 
                                        (selected-item-changed)="getDdlDataForCountry($event.detail.value.code)"
                            ></vaadin-combo-box>
                    <vaadin-combo-box   auto-validate ="true" name="country" [readonlyCustomGroups]="groupRights===false" 
                                        error-message="Field required!" required class="elements-box" [items]="COUNTRY"
                                        class="paddingInput"
                                        item-label-path="name"
                                        item-value-path="code"
                                        [disabled]="!COUNTRY" 
                                        [(ngModel)]="obj.country"
                                        [value]="obj.countryCode" 
                                        ngDefaultControl 
                                        (selected-item-changed)="obj.countryCode=$event.detail.value.code"
                                        label="Country" ></vaadin-combo-box>
                    <paper-input label="Type" auto-validate ="true" name="type" required="true" error-message="Field required!" class="paddingInput" [(ngModel)]="obj.groupTypeCode" readonly="true" ngDefaultControl></paper-input>

               </form>

If i use the reset() method of the form, it does trigger the validations but also removes all data from the fields and i dont want that. 如果我使用表单的reset()方法,它会触发验证,但还会从字段中删除所有数据,而我不希望这样做。

Am i doing something wrong? 难道我做错了什么? Is there something else i should do? 还有什么我应该做的吗?

It seems that because they are polymer elements, there are still some issues when implementing them with the help of Angular2-Polymer. 似乎因为它们是聚合物元素,所以在Angular2-Polymer的帮助下实现它们时仍然存在一些问题。

I fixed my problem by calling the "validate()" polymer method on each element that was a child of the form. 我通过在作为表单子元素的每个元素上调用“ validate()”聚合物方法来解决问题。

The error css classes and message now appear. 现在出现错误CSS类和消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM