Access validation information from Angular 2 model-driven form? -
i have angular 2 form uses template-driven approach converting model-driven approach. currently, form uses style this:
.ng-valid[required] { border-left: 5px solid #42a948; /* green */ }
this marks of fields have required attribute green bar.
now moving model-driven, control no longer has "required" attribute. there way access validation rules associated control (see below) binding in form?
buildform() { this.heroform = this.fb.group({ 'name': [this.model.name, validators.compose([validators.required, validators.minlength(4), validators.maxlength(24)])], 'alterego': [this.model.alterego], 'power': [this.model.power, validators.required] });
i able provide logic using set of internal data structures , building method this:
isrequired(controlname: string): boolean { if (object.keys(this._validationmessages).includes(controlname)) { return object.keys(this._validationmessages[controlname]).includes('required');} return false; }
the template binds method, passing in name of control.