Display custom validator error with mat-error

后端 未结 3 1754
清酒与你
清酒与你 2021-01-30 11:16

I come to you for talking about a problem with angular material. In fact, I think it\'s an issue, but I prefer looking for a misunterstanding first.

The first thing abou

3条回答
  •  情深已故
    2021-01-30 11:33

    obsessiveprogrammer's answer was correct for me, however I had to change the childrenEqual function with angular 6 and strictNullChecks (which is an option recommended by the angular team) to this:

    static childrenEqual: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
            const f = control as FormGroup;
    
            const [firstControlName, ...otherControlNames] = Object.keys(f.controls || {});
    
            if(f.get(firstControlName) == null) {
                return null;
            }
    
            otherControlNames.forEach(controlName => {
                if(f.get(controlName) == null) {
                    return null;
                }
            })
    
            const isValid = otherControlNames.every(controlName => f.get(controlName)!.value === f.get(firstControlName)!.value);
            return isValid ? null : { childrenNotEqual: true };
        }
    

提交回复
热议问题