Search code examples
angularangular-formsangular-validation

Angular forms: validate complete form group if a single value changes


Is it possible to add a single validator to a formGroup, which is beeing called as soon as a single value inside the formGroup changes?

I think it should look like this:

'group': this.formBuilder.group({
    'value1': [''],
    'value2': [''],
    'value3': ['']
  }, [MyCustomValidator]),

but the validator is not invoked on changes happening inside the formGroup,... I can add the validator to each field individually and then call the control.parent (which is working), but this doesn't seem to be like a proper solution.


Solution

  • In this way of using the validator, it's fired each time value1 and value2 get their values changed:

     this.fb.group({
                value1: ['', [this.validateService.isNumber]],
                value2: ['', [this.validateService.isNumber, this.validateService.isLessThanHundred]],
            }, { validator: this.validateService.countInputs });
    

    plunker