如何根据复选框选择验证表单中的字段?

时间:2017-04-05 05:12:26

标签: angular angularjs-directive angularjs-scope

我有一个客户表单,用户可以在其中输入他们的联系方式。此外,还有备用联系人详细信息的复选框。我必须验证这两个部分中的所有必填字段。我写得像这样

constructor(private api: ApiService, @Inject(FormBuilder) formBuilder: FormBuilder, validatorService: ValidatorService) {
    this.firstName = new FormControl('', Validators.required);
    this.surName = new FormControl('', Validators.required);
    this.address1 = new FormControl('', Validators.required);
    this.address2 = new FormControl('');
    this.zip = new FormControl('', Validators.required);
    this.city = new FormControl('', Validators.required);
    this.state = new FormControl('',validatorService.SelectValidation);

    this.phoneNumber = new FormControl('');
    this.email = new FormControl('');
    this.codeOfOrigin = new FormControl('');
    this.hasAlternateDeliveryAddress = new FormControl('');
    this.isClubMember = new FormControl('');
    this.needNewsletter = new FormControl('');
    this.registerUser = new FormControl('');           
    this.firstNameAlternate = new FormControl('', Validators.required);
    this.surNameAlternate = new FormControl('', Validators.required);
    this.address1Alternate = new FormControl('', Validators.required);
    this.address2Alternate = new FormControl('');
    this.zipAlternate = new FormControl('', Validators.required);
    this.cityAlternate = new FormControl('', Validators.required);
    this.stateAlternate = new FormControl('', validatorService.SelectValidation);
    this.customerFormGroup = formBuilder.group({
        'firstName': this.firstName,
        'surName': this.surName,
        'address1': this.address1,
        'address2': this.address2,
        'zip': this.zip,
        'city': this.city,
        'state': this.state,
        'phoneNumber': this.phoneNumber,
        'email': this.email,
        'codeOfOrigin': this.codeOfOrigin,
        'hasAlternateDeliveryAddress': this.hasAlternateDeliveryAddress,
        'isClubMember': this.isClubMember,
        'needNewsletter': this.needNewsletter,
        'registerUser': this.registerUser,
        'firstNameAlternate': this.firstNameAlternate,
        'surNameAlternate': this.surNameAlternate,
        'address1Alternate': this.address1Alternate,
        'address2Alternate': this.address2Alternate,
        'zipAlternate': this.zipAlternate,
        'cityAlternate': this.cityAlternate,
        'stateAlternate': this.stateAlternate
    });       

}   

但是在这里,所有字段都在验证中。如果用户未选中此复选框,如何限制备用联系人详细信息的验证?

0 个答案:

没有答案
相关问题