Angular 2表单状态对于ng-select控件的自定义验证始终无效

Ibrahim Shah |

我已经在以下用户信息表单中使用了验证。

this.userInfoForm = this.fb.group({
    retrieveId: [''],
    customerName: [[],[UtilityService.checkMinLength(3, 50)]],
});

我已经创建了以下服务来检查验证

 @Injectable()
 export class UtilityService {
     static checkMinLength(min: number, max: number): ValidatorFn {
         return (c: AbstractControl) => {
             if (c.value && c.value != undefined) {
                 return {
                     'checkMinLength': c.value.some((a) => {
                         if (a.itemName.length < min || a.itemName.length > max) { return true; }
                         return null;
                      })
                 };
            }
        }
    }

在HTML中使用checkMinLength来检查customername字段的验证这些验证工作正常,但是当我检查表单状态时显示“ INVALID”。ng-select控件始终禁用“提交”按钮

<form class="form-horizontal" novalidate (ngSubmit)="saveInfo()" [formGroup]="userInfoForm" autocomplete="off">
    <fieldset>
        <div class="form-group padding-top-bottom" [ngClass]="{'has-error': (userInfoForm.get('customerName').touched || userInfoForm.get('customerName').dirty) &&!userInfoForm.get('customerName').valid }">
            <label class="col-md-4" for="customerNameId" tooltip={{attributeNames.customerNameTitle}} data-placement="right">Customer Name</label>
            <div class="col-md-7">
                <ng-select [items]="customerName" multiple="true" [addTag]="true" bindLabel="itemName" (change)="onItemSelect($event,'customer','customerName')" formControlName="customerName" [(ngModel)]="customerName"></ng-select>
                <span class="help-block" *ngIf="userInfoForm.get('customerName').touched ||userInfoForm.get('customerName').dirty) &&userInfoForm.get('customerName').errors">
                    <span *ngIf="userInfoForm.get('customerName').errors.checkMinLength">End Customer Name must be longer than 3 characters.</span>
                </span>
           </div>
       </div>
       <button class="btn btn-primary" id="submitForm" type="submit" [disabled]="!userInfoForm.valid || !userInfoForm.dirty">Save</button>
    </fieldset>
</form>
姆鲁瓦诺娃

如果控件有效,则您的验证器必须返回“ null”(不是具有null属性的对象)

export class UtilityService {
 static checkMinLength(min: number, max: number): ValidatorFn {
     return (c: AbstractControl) => {
         if (c.value && c.value != undefined) {
             return {
                 'checkMinLength': c.value.some((a) => {
                     if (a.itemName.length < min || a.itemName.length > max) { return true; }
                  })
             };
        }
        return null; // LOOK HERE
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Angular2模板驱动的表单:如何为自定义表单控件创建字段验证?

Angular 4:反应式表单控件通过自定义异步验证器停留在待处理状态

使用自定义验证程序ng-valid类进行Angular 2表单验证

Angular 2自定义复合控件

Angular 2-自定义表单控件-禁用

在Angular 2中创建自定义表单控件

Angular Reactive Forms:是否可以创建包含“必须具备”验证的自定义表单控件组件?

Angular自定义表单验证器使用其他控件

Angular 2反应式表单自定义验证器仅在表单控件有效时适用

Angular 表单自定义验证数据

Angular2如何将参数传递到自定义表单控件验证器中?

依赖于另一个表单控件的Angular 2自定义验证器

Angular 2 反应式表单自定义验证

带有验证错误(mat-error)的Angular6 Material自定义表单字段控件

Django自定义表单验证无效

Angular 5 表单自定义验证无法正常工作

如何应用自定义表单验证[Angular]

在自定义表单验证中使用Angular的`useExitsing`吗?

Angular 2自定义表单控件,以模板驱动方式具有多个输入

Angular 2-单元测试绑定到嵌套自定义表单控件

Angular 2自定义表单输入

Angular 2 routerLinkActive对于基本路径始终处于活动状态

Angular 2+ 自定义表单验证打破了表单方法

angular2中的简单自定义验证

Angular 2-自定义验证器语法

带有参数的Angular 2自定义验证器

Angular 2强制对键进行自定义验证

Angular 2从自定义验证器检索错误消息

Angular2自定义验证器未调用