Skip to content
This repository was archived by the owner on Jan 5, 2022. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
OnInit,
AfterContentInit
} from "@angular/core";
import { FormControlName } from "@angular/forms";
import { FormControl, FormControlName } from "@angular/forms";
import { ErrorMessageService } from "../../Services/error-message.service";
import { MessagesComponent } from "../messages/messages.component";
import { ErrorMessage } from "../../Models/error-message";
Expand All @@ -26,6 +26,9 @@ export class FormGroupComponent implements OnInit, AfterContentInit {
@ContentChildren(FormControlName)
FormControlNames: QueryList<FormControlName>;

@ContentChildren(FormControl)
FormControls: QueryList<FormControl>;

@Input()
customErrorMessages: ErrorMessage[] = [];

Expand All @@ -35,17 +38,20 @@ export class FormGroupComponent implements OnInit, AfterContentInit {
@HostBinding("class.has-error")
get hasErrors() {
return (
this.FormControlNames.some(c => !c.valid && c.dirty && c.touched) &&
!this.validationDisabled
!this.validationDisabled &&
(this.FormControlNames.some(c => !c.valid && c.dirty && c.touched) ||
this.FormControls.some(c => !c.valid && c.dirty && c.touched))
);
}

@HostBinding("class.has-success")
get hasSuccess() {
return (
!this.FormControlNames.some(c => !c.valid) &&
this.FormControlNames.some(c => c.dirty && c.touched) &&
!this.validationDisabled
!this.validationDisabled &&
(!this.FormControlNames.some(c => !c.valid) ||
!this.FormControls.some(c => !c.valid)) &&
(this.FormControlNames.some(c => c.dirty && c.touched) ||
this.FormControls.some(c => c.dirty && c.touched))
);
}

Expand Down Expand Up @@ -79,7 +85,10 @@ export class FormGroupComponent implements OnInit, AfterContentInit {
}

get isDirtyAndTouched() {
return this.FormControlNames.some(c => c.dirty && c.touched);
return (
this.FormControlNames.some(c => c.dirty && c.touched) ||
this.FormControls.some(c => c.dirty && c.touched)
);
}

private getMessages(): string[] {
Expand All @@ -90,13 +99,20 @@ export class FormGroupComponent implements OnInit, AfterContentInit {

const names = this.FormControlNames.map(f => f.name);

this.FormControlNames.filter(
const formControlNames = this.FormControlNames.filter(
(c, i) =>
!c.valid &&
!!c.errors &&
// filter out FormControlNames that share the same name - usually for radio buttons
names.indexOf(c.name) === i
).forEach(control => {
);

const formControls = this.FormControls.filter(
c =>
!c.valid && !!c.errors && !formControlNames.some(cn => cn.control === c)
);

[...formControlNames, ...formControls].forEach(control => {
Object.keys(control.errors).forEach(key => {
const error = this.errorMessages.find(err => err.error === key);
if (!error) {
Expand Down