Can you use a string variable to set angular material components ValidationErrors?

Viewed 25

If I make a custom validator for a control I can set the errors like this

        if (!isValid) {
            Object.keys(formgroup.controls).forEach((key: string) => {
                const abstractControl = formgroup.controls[key];
                if (!abstractControl.getError("pattern")) 
                    abstractControl.setErrors({ childrenNotEqual: true });
                else 
                    abstractControl.setErrors([{ childrenNotEqual: true }, { pattern: true }]);
            });
        }

getError and setErrors work on the ValidationErrors type that is defined like this:

export declare type ValidationErrors = {
    [key: string]: any;
};

Now what can I do if instead of setting pattern, I want to make that variable? Because doing this does not work, it sets an error called test:

var test: string = "pattern";
abstractControl.setErrors([{ childrenNotEqual: true }, { test: true }]);

And then finally I would make 'test' a settable property or something, not a local variable of course.

0 Answers
Related