I have a number of custom validators that will apply to fields in a dynamically generated Reactive Form in Angular 7.
The template looks like this:
<ng-container class="table-wrapper" *ngIf="!field.hide && field.display === fieldTypes.get('Text')">
<label>
<span class="form-label">{{field.label}}</span>
<span *ngIf="dynForm.controls[field.name].invalid
&& ( dynForm.controls[field.name].dirty || dynForm.controls[field.name].touched )">
{{errorMessageResolver(field, dynForm.controls[field.name].errors)}}
</span>
</label>
<input type="text" name="{{field.name}}" value="{{field.val}}" formControlName="{{field.name}}"/>
</ng-container>
As you can see, I have a function that pulls out the generated ValidationError from the form and feeds it into the following function:
errorMessageResolver(field: Field, errors: ValidationErrors) {
console.log('valids=' + field.validations.length);
console.log(errors);
console.log(errors as ValidationErrors);
console.log(errors.hasOwnProperty('required'));
console.log(errors.hasOwnProperty('minLength'));
console.log(errors.minLength);
console.log(errors.hasOwnProperty('maxLength'));
}
The result of the extensive console logging is:
As I hope you can see, the minLength ValidationError is definitely being picked up, because you can see the console output:
{ minLength: {...}}
But the following attempted consoles should show how there doesn't seem to be a way to get hold of the actual value. So how do you get hold of the key and its value?