Javascript 多个输入字段之间的表单验证

Javascript 多个输入字段之间的表单验证,javascript,forms,angular,validation,angular4-forms,Javascript,Forms,Angular,Validation,Angular4 Forms,因此,我有一个具有多个输入表单字段的表单和一个嵌套表单组。在嵌套的formgroup中,验证应该是这样的:如果三个输入中的任何一个被填充,那么表单应该是有效的。此人可以填写全部,甚至可以只填写一个,并且表格应有效。我的模板代码如下: <h3 class="form-title">{{title}}</h3> <form [formGroup]="formX" novalidate> <div formGroupName="brandIdenti

因此,我有一个具有多个输入表单字段的表单和一个嵌套表单组。在嵌套的formgroup中,验证应该是这样的:如果三个输入中的任何一个被填充,那么表单应该是有效的。此人可以填写全部,甚至可以只填写一个,并且表格应有效。我的模板代码如下:

 <h3 class="form-title">{{title}}</h3>
<form [formGroup]="formX" novalidate>
    <div formGroupName="brandIdentifier">
        <mat-form-field class="full-width">
            <mat-icon matSuffix color="primary" *ngIf="brandName.valid && brandName.touched">done</mat-icon>
            <mat-icon matSuffix color="primary" *ngIf="brandName.invalid && brandName.touched">close</mat-icon>
            <input matInput type="text" placeholder="Brand Name" formControlName="brandName" />
        </mat-form-field>
        <mat-form-field class="full-width">
            <mat-icon matSuffix color="primary" *ngIf="brandId.valid && brandId.touched">done</mat-icon>
            <mat-icon matSuffix color="primary" *ngIf="brandId.invalid && brandId.touched">close</mat-icon>
            <input matInput type="text" placeholder="Brand Id" formControlName="brandId" />
        </mat-form-field>
        <mat-form-field class="full-width">
            <mat-icon matSuffix color="primary" *ngIf="contentId.valid && contentId.touched">done</mat-icon>
            <mat-icon matSuffix color="primary" *ngIf="contentId.invalid && contentId.touched">close</mat-icon>
            <input matInput type="text" placeholder="Content Id" formControlName="contentId" />
        </mat-form-field>
    </div>
    <mat-form-field class="full-width">
        <mat-icon matSuffix color="primary" *ngIf="startTime.valid && startTime.touched">done</mat-icon>
        <mat-icon matSuffix color="primary" *ngIf="startTime.invalid && startTime.touched">close</mat-icon>
        <input matInput type="text" placeholder="Start Time" formControlName="startTime" />
    </mat-form-field>
    <mat-form-field class="full-width">
        <mat-icon matSuffix color="primary" *ngIf="endTime.valid && endTime.touched">done</mat-icon>
        <mat-icon matSuffix color="primary" *ngIf="endTime.invalid && endTime.touched">close</mat-icon>
        <input matInput type="text" placeholder="End Time" formControlName="endTime" />
    </mat-form-field>

    <button mat-raised-button color="primary" (click)="startAnalysis()" [ngClass]="{'form-valid':formX.valid, 'form-invalid':formX.invalid}">Submit</button>

    <pre>{{formX.value | json}}</pre>
</form>
this.myForm = this.fb.group({
  nestedGroup: this.fb.group({
    first: [''],
    second: [''],
    third: ['']
    // apply custom validator for the nested group only
  }, {validator: this.myCustomValidator})
});
{{title}
完成
关闭
完成
关闭
完成
关闭
完成
关闭
完成
关闭
提交
{{formX.value | json}

我该怎么办?使用验证器类是给定的,但我无法将其设置为可选的。

请按照以下步骤操作:

this.myForm = this.fb.group({
  nestedGroup: this.fb.group({
    first: [''],
    second: [''],
    third: ['']
    // apply custom validator for the nested group only
  }, {validator: this.myCustomValidator})
});
注:我们只是想告诉您如何解决您的问题

this.myForm = this.fb.group({
  nestedGroup: this.fb.group({
    first: [''],
    second: [''],
    third: ['']
    // apply custom validator for the nested group only
  }, {validator: this.myCustomValidator})
});
  • 在module.ts中导入FormsModule、ReactiveFormsModule

    this.myForm = this.fb.group({
      nestedGroup: this.fb.group({
        first: [''],
        second: [''],
        third: ['']
        // apply custom validator for the nested group only
      }, {validator: this.myCustomValidator})
    });
    
    从'@angular/forms'导入{FormsModule,ReactiveFormsModule}

    this.myForm = this.fb.group({
      nestedGroup: this.fb.group({
        first: [''],
        second: [''],
        third: ['']
        // apply custom validator for the nested group only
      }, {validator: this.myCustomValidator})
    });
    
    @NGD模块({ 导入:[FormsModule,ReactiveFormsModule]})

  • this.myForm = this.fb.group({
      nestedGroup: this.fb.group({
        first: [''],
        second: [''],
        third: ['']
        // apply custom validator for the nested group only
      }, {validator: this.myCustomValidator})
    });
    
  • 根据给定内容修改html,例如:

    this.myForm = this.fb.group({
      nestedGroup: this.fb.group({
        first: [''],
        second: [''],
        third: ['']
        // apply custom validator for the nested group only
      }, {validator: this.myCustomValidator})
    });
    
    用户名 用户名是必需的。 用户名的长度必须至少为4个字符。 密码 密码是必需的。 密码长度必须至少为6个字符。 忘记密码?

    this.myForm = this.fb.group({
      nestedGroup: this.fb.group({
        first: [''],
        second: [''],
        third: ['']
        // apply custom validator for the nested group only
      }, {validator: this.myCustomValidator})
    });
    
    
    登录
    
  • 在[yourcomponent]中修改如下代码。例如:

    this.myForm = this.fb.group({
      nestedGroup: this.fb.group({
        first: [''],
        second: [''],
        third: ['']
        // apply custom validator for the nested group only
      }, {validator: this.myCustomValidator})
    });
    
    从'@angular/core'导入{Component}; 从“@angular/forms”导入{FormBuilder、FormGroup、Validators}

    this.myForm = this.fb.group({
      nestedGroup: this.fb.group({
        first: [''],
        second: [''],
        third: ['']
        // apply custom validator for the nested group only
      }, {validator: this.myCustomValidator})
    });
    
    @组成部分({ 选择器:“应用程序登录”, templateUrl:'./login.component.html', 样式URL:['./login.component.css'] }) 导出类登录组件{

    this.myForm = this.fb.group({
      nestedGroup: this.fb.group({
        first: [''],
        second: [''],
        third: ['']
        // apply custom validator for the nested group only
      }, {validator: this.myCustomValidator})
    });
    
    loginForm:FormGroup

    this.myForm = this.fb.group({
      nestedGroup: this.fb.group({
        first: [''],
        second: [''],
        third: ['']
        // apply custom validator for the nested group only
      }, {validator: this.myCustomValidator})
    });
    
    构造函数(私有fb:FormBuilder){ 此文件名为.loginform() }

    this.myForm = this.fb.group({
      nestedGroup: this.fb.group({
        first: [''],
        second: [''],
        third: ['']
        // apply custom validator for the nested group only
      }, {validator: this.myCustomValidator})
    });
    
    get userName(){返回this.loginForm.get('userName');} get password(){返回this.loginForm.get('password');}

    this.myForm = this.fb.group({
      nestedGroup: this.fb.group({
        first: [''],
        second: [''],
        third: ['']
        // apply custom validator for the nested group only
      }, {validator: this.myCustomValidator})
    });
    
    crateLoginForm(){ this.loginForm=this.fb.group({ 用户名:['',[Validators.required,Validators.minLength(4)], 密码:[''[Validators.required,Validators.minLength(6)]] }) }

    this.myForm = this.fb.group({
      nestedGroup: this.fb.group({
        first: [''],
        second: [''],
        third: ['']
        // apply custom validator for the nested group only
      }, {validator: this.myCustomValidator})
    });
    
    onSubmit(){ 让userName=this.loginForm.value.userName; 让pwd=this.loginForm.value.password; }

    this.myForm = this.fb.group({
      nestedGroup: this.fb.group({
        first: [''],
        second: [''],
        third: ['']
        // apply custom validator for the nested group only
      }, {validator: this.myCustomValidator})
    });
    
    }

  • this.myForm = this.fb.group({
      nestedGroup: this.fb.group({
        first: [''],
        second: [''],
        third: ['']
        // apply custom validator for the nested group only
      }, {validator: this.myCustomValidator})
    });
    

    您可以为此使用自定义验证器,为嵌套组应用自定义验证器,其中我们检查三个字段中至少有一个字段的值不是空字符串,因此下面是一个示例:

    this.myForm = this.fb.group({
      nestedGroup: this.fb.group({
        first: [''],
        second: [''],
        third: ['']
        // apply custom validator for the nested group only
      }, {validator: this.myCustomValidator})
    });
    
    构建形式:

    this.myForm = this.fb.group({
      nestedGroup: this.fb.group({
        first: [''],
        second: [''],
        third: ['']
        // apply custom validator for the nested group only
      }, {validator: this.myCustomValidator})
    });
    
     myCustomValidator(group: FormGroup) {
       // if true, all fields are empty
       let bool = Object.keys(group.value).every(x => group.value[x] === '')
       if(bool) {
         // return validation error
         return { allEmpty: true}
       }
       // valid
       return null;
     }
    
    自定义验证程序:

    this.myForm = this.fb.group({
      nestedGroup: this.fb.group({
        first: [''],
        second: [''],
        third: ['']
        // apply custom validator for the nested group only
      }, {validator: this.myCustomValidator})
    });
    
    <small *ngIf="myForm.hasError('allEmpty','nestedGroup')">
      Please fill at least one field
    </small>
    
    然后在表单中,您可以通过以下方式显示验证消息:

    this.myForm = this.fb.group({
      nestedGroup: this.fb.group({
        first: [''],
        second: [''],
        third: ['']
        // apply custom validator for the nested group only
      }, {validator: this.myCustomValidator})
    });
    
    
    请至少填写一个字段
    
    最后是一个:)

    this.myForm = this.fb.group({
      nestedGroup: this.fb.group({
        first: [''],
        second: [''],
        third: ['']
        // apply custom validator for the nested group only
      }, {validator: this.myCustomValidator})
    });