2

I am using reactive forms Angular 4 and added a custom validator addressValidation to the form group - addressGroup. I am updating all fields to mark as touched on submit click. Looks like the custom validator addressValidation doesn't trigger eventhough I marked all fields as touched. I tried marking the formgroup (addressGroup) as touched and dirty on submit but no help.

In general what I am trying to achieve is - By default I want to make street number and Street name required. If po box is entered then street number and Name is not required. Apt # is only required only if street number and name is entered. I am trying to achieve this on the custom validator in the formGroup.

Any idea on what I am doing wrong. Any other alternate way to achieve the above requirement. I am new to Angular and slowly learning the concepts. Any suggestion on How to trigger the custom validator on submit.

     buildForm(): void {
            this.contactForm = this.fb.group({
                emailAddressControl: ['', [Validators.required, Validators.email, Validators.maxLength(100)]],
                phoneControl: ['', [Validators.required, Validators.minLength(10), Validators.maxLength(10)]],            
                addressGroup: this.fb.group({
                    streetNumber: ['', [Validators.maxLength(10)]],
                    pOBox: ['', [Validators.maxLength(8)]],
                    aptNumber: ['', [Validators.maxLength(8)]],
                    streetName: ['', [Validators.maxLength(60)]],
                    cityControl: ['', [Validators.required, Validators.maxLength(50)]],
                    stateControl: ['', [Validators.required, Validators.maxLength(2)]],
                    zipControl: ['', [Validators.required, Validators.maxLength(14)]],
                    countryControl: ['UNITED STATES OF AMERICA', [Validators.required]],
                }, { validator: addressValidation })
            })
            this.contactForm.valueChanges
                .debounceTime(800)
                .subscribe(data => this.onValueChanged(data));
            this.onValueChanged();
        }


    onSubmit(): void {


            this.markAllFormFieldsAsTouched(this.contactForm);
            this.onValueChanged();

    }

   private markAllFormFieldsAsTouched(formGroup: FormGroup) {
        Object.keys(formGroup.controls).forEach(field => {
            console.log(field);
            const control = formGroup.get(field);
            if (control instanceof FormControl) {
                control.markAsTouched({ onlySelf: true });
            }
            else if (control instanceof FormGroup) {
                this.markAllFormFieldsAsTouched(control);
                control.markAsTouched({ onlySelf: true });
            }
            else if (control instanceof FormArray) {
                for (let formgroupKey in control.controls) {
                    let formgroup = control.controls[formgroupKey];
                    if (formgroup instanceof FormGroup) {
                        this.markAllFormFieldsAsTouched(formgroup);
                    }
                }
            }

        });
    }

    function addressValidation(c: AbstractControl): { [key: string]: boolean } | null {
        if (c.pristine) {
            return null;
        }
        const pOBoxControl = c.get('pOBox');
        const streetNameControl = c.get('streetName');
        const streetNumberControl = c.get('streetNumber');
        const aptNumberControl = c.get('aptNumber');
        if (pOBoxControl.value === null || pOBoxControl.value === "") {
            if (streetNumberControl.value === null || streetNumberControl.value === "") {
                return { ['streetNumberRequired']: true, ['streetNameRequired']: true };
            }
            if (streetNameControl.value === null || streetNameControl.value === "") {
                return { 'streetNameRequired': true };
            }
        }
        else {
            if ((streetNameControl.value === null || streetNameControl.value === "")
                && (streetNameControl.value === null || streetNumberControl.value === "") && aptNumberControl.value !== "") {
                return { 'apartmentNumberInvalid': true };
            }
        }
    }

Template

 <div class="card">
            <div class="card-header bg-info text-white">
                <h2>Mailing Address:</h2>
            </div>
            <div formGroupName="addressGroup" class="card-body">
                <div class="row">
                    <div class="col-lg-4">
                        <div class="form-group">
                            <label class="form-control-label">PO Box:</label>
                            <input class="form-control"
                                   [ngClass]="displayFieldCss('pOBox')"
                                   type="text"
                                   formControlName="pOBox"
                                   placeholder=""
                                   maxlength="8" />
                            <span class="invalid-feedback" *ngIf="isValidToDisplayErrors('pOBox')">
                                {{validationMessage.pOBox}}
                            </span>
                        </div>
                    </div>
                    <div class="col-lg-4">
                        <div class="form-group">
                            <label class="form-control-label">Street Number:</label>
                            <input class="form-control"
                                   [ngClass]="displayFieldCss('streetNumber')"
                                   type="text"
                                   formControlName="streetNumber"
                                   placeholder=""
                                   maxlength="10" />
                            <span class="invalid-feedback" *ngIf="isValidToDisplayErrors('streetNumber')">
                                {{validationMessage.streetNumber}}
                            </span>
                        </div>
                    </div>
                    <div class="col-lg-4">
                        <div class="form-group">
                            <label class="form-control-label">Apt Number:</label>
                            <input class="form-control"
                                   [ngClass]="displayFieldCss('aptNumber')"
                                   type="text"
                                   formControlName="aptNumber"
                                   placeholder=""
                                   maxlength="8" />
                            <span class="invalid-feedback" *ngIf="isValidToDisplayErrors('aptNumber')">
                                {{validationMessage.aptNumber}}
                            </span>
                        </div>
                    </div>                    
                </div>
                <div class="row">
                    <div class="col-lg-12">
                        <div class="form-group">
                            <label class="form-control-label">Street Name:</label>
                            <input class="form-control"
                                   [ngClass]="displayFieldCss('streetName')"
                                   type="text"
                                   formControlName="streetName"
                                   placeholder=""
                                   maxlength="60" />
                            <span class="invalid-feedback" *ngIf="isValidToDisplayErrors('streetName')">
                                {{validationMessage.streetName}}
                            </span>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-5">
                        <div class="form-group">
                            <label class="form-control-label">City:</label>
                            <input class="form-control"
                                   [ngClass]="displayFieldCss('cityControl')"
                                   type="text"
                                   formControlName="cityControl"
                                   placeholder="(required)"
                                   maxlength="50" />
                            <span class="invalid-feedback" *ngIf="isValidToDisplayErrors('cityControl')">
                                {{validationMessage.cityControl}}
                            </span>
                        </div>
                    </div>
                    <div class="col-lg-4">
                        <div class="form-group">
                            <label class="form-control-label">State/Province (Code):</label>
                            <input class="form-control"
                                   [ngClass]="displayFieldCss('stateControl')"
                                   type="text"
                                   formControlName="stateControl"
                                   placeholder="(required)"
                                   maxlength="3" />
                            <span class="invalid-feedback" *ngIf="isValidToDisplayErrors('stateControl')">
                                {{validationMessage.stateControl}}
                            </span>
                        </div>
                    </div>
                    <div class="col-lg-3">
                        <div class="form-group">
                            <label class="form-control-label">Zip:</label>
                            <input class="form-control"
                                   [ngClass]="displayFieldCss('zipControl')"
                                   type="text"
                                   formControlName="zipControl"
                                   placeholder="(required)"
                                   maxlength="14" />
                            <span class="invalid-feedback" *ngIf="isValidToDisplayErrors('zipControl')">
                                {{validationMessage.zipControl}}
                            </span>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-12">
                        <div class="form-group">
                            <label class="form-control-label">Country:</label>
                            <input class="form-control"
                                   [ngClass]="displayFieldCss('countryControl')"
                                   type="text"
                                   formControlName="countryControl"
                                   placeholder="(required)"
                                   maxlength="50" />
                            <span class="invalid-feedback" *ngIf="isValidToDisplayErrors('countryControl')">
                                {{validationMessage.countryControl}}
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
zgue
  • 3,793
  • 9
  • 34
  • 39
Vinod
  • 343
  • 1
  • 4
  • 14
  • Is any of your validations working? Also, please include all the relevant code sections of your formGroups usage in component and template – amal Nov 10 '17 at 16:31
  • Yes all other validaions firing (required , maxlength etc). I put a breakpoint on the address validation function and its not getting called eventhough I updated all fields as touched. It gets called as soon as I start typing something. – Vinod Nov 10 '17 at 17:15
  • Do you have `formGroupName=addressGroup` declaration in your form template? Also, it's still not evident how you initiate your form (can't see when you call `buildForm()` method) and also when `onSubmit()` is called – amal Nov 10 '17 at 17:28
  • In the buildform I created a form address group. I updated the template where I set formGroupName=addressGroup on card body of address elements. OnSubmit I am setting all fields (including the ones inside addressGroup) to Touched. Setting Touched to true should trigger the custom validator on form group correct? Am I missing something? Thanks for your time. – Vinod Nov 10 '17 at 17:52
  • I don't think it will trigger validations unless the value on the corresponding formcontrol is changed. Just clicking on a button shouldn't trigger validation as the value hasn't changed on the field (when a button is pressed), and hence no need to trigger validation. Why do you expect the value to change upon a button press? – amal Nov 10 '17 at 18:04
  • I understand your point. Any suggestion on how to implement a required validation on street name and Street number ONLY if PO box is left empty. – Vinod Nov 10 '17 at 18:21
  • Please check [this answer](https://stackoverflow.com/a/46183057/5260710) for ideas on how to implement it. Towards the end, it talks about how to set and clear validators specifically on formControls within a formGroup. Hope it helps. – amal Nov 10 '17 at 18:31

1 Answers1

2

You must use control.updateValueAndValidity() like this

onSubmit(): void {
 if (this.form.valid) {
 
 } else {
  this.validateAllFormFields(this.committeForm); 
  this.logValidationErrors(this.committeForm);
  this.scrollToError();
 }
}
validateAllFormFields(formGroup: FormGroup) {       
  Object.keys(formGroup.controls).forEach(field => {  
    const control = formGroup.get(field);             
    if (control instanceof FormControl) {             
      control.updateValueAndValidity()
    } else if (control instanceof FormGroup) {        
      this.validateAllFormFields(control);            
    }
  });
}
Random
  • 3,158
  • 1
  • 15
  • 25
Musab.BA
  • 400
  • 7
  • 16