I\'m using Material 2 in my app, but in this question I want to solve a problem specifically with Input.
As you can see in API Reference there\'s a
I adjusted the code from joh04667 and HankCa to this:
export const hasValidator = (form: FormGroup, controlPath: string, validator: string): boolean => {
const control = form.get(controlPath);
const validators = control.validator(control);
return !!(validators && validators.hasOwnProperty(validator));
};
Which I store in a file called util.ts and import in the component that contains the form like this:
import * as util from '@app/shared/util';
And define util in your class:
public util = util;
Add the directive to your input component like this:
[required]="util.hasValidator(myForm, 'path.to.control', 'required')"