Skip to content

Commit bbd934d

Browse files
Validation working both at rangeInputComponent and numberRangeFilterComponent
1 parent f954f9a commit bbd934d

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<mat-form-field>
22
<mat-label>{{ filterConfig.label || filterConfig.name }}</mat-label>
3-
<app-range-input [formControl]="formControl"></app-range-input>
3+
<app-range-input
4+
[formControl]="formControl"
5+
[deactivateValidation]="false"
6+
></app-range-input>
47
</mat-form-field>

src/app/core/basic-datatypes/number/number-range-filter/number-range-filter.component.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { Component, Input } from "@angular/core";
22
import { Entity } from "../../../entity/model/entity";
33
import { MatFormFieldModule } from "@angular/material/form-field";
4-
import { FormControl, ReactiveFormsModule } from "@angular/forms";
4+
import {
5+
FormControl,
6+
ReactiveFormsModule,
7+
ValidationErrors,
8+
ValidatorFn,
9+
} from "@angular/forms";
510
import {
611
NumericRange,
712
RangeInputComponent,
@@ -20,11 +25,26 @@ export class NumberRangeFilterComponent<T extends Entity> {
2025

2126
formControl: FormControl<NumericRange>;
2227

28+
private validatorFunction: ValidatorFn = (): ValidationErrors | null => {
29+
if (
30+
this.formControl.value.from &&
31+
this.formControl.value.to &&
32+
this.formControl.value.from === this.formControl.value.to
33+
) {
34+
return {
35+
sameValue: "The from value is the same as the to value.",
36+
};
37+
} else {
38+
return null;
39+
}
40+
};
41+
2342
constructor() {
2443
this.formControl = new FormControl<NumericRange>({ from: 0, to: 1 });
44+
this.formControl.addValidators([this.validatorFunction]);
2545

2646
this.formControl.valueChanges.subscribe((value) => {
27-
console.log("external value changes", value);
47+
console.log(this.formControl.valid, this.formControl.errors);
2848
});
2949
}
3050
}

src/app/core/basic-datatypes/number/number-range-filter/range-input/range-input.component.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { Component, ElementRef, Input, Optional, Self } from "@angular/core";
22
import {
33
FormControl,
4+
FormControlDirective,
45
FormGroup,
56
FormGroupDirective,
67
NgControl,
78
NgForm,
89
ReactiveFormsModule,
10+
ValidationErrors,
11+
ValidatorFn,
912
} from "@angular/forms";
1013
import { ErrorStateMatcher } from "@angular/material/core";
1114
import { MatFormFieldControl } from "@angular/material/form-field";
@@ -27,6 +30,7 @@ export class RangeInputComponent extends CustomFormControlDirective<NumericRange
2730
from: new FormControl(),
2831
to: new FormControl(),
2932
});
33+
formControlDirective: FormControlDirective;
3034

3135
@Input() set value(value: NumericRange) {
3236
// update the internal formGroup when the value changes from the outside
@@ -37,12 +41,16 @@ export class RangeInputComponent extends CustomFormControlDirective<NumericRange
3741
return super.value;
3842
}
3943

44+
@Input()
45+
deactivateValidation: boolean = false;
46+
4047
constructor(
4148
elementRef: ElementRef<HTMLElement>,
4249
errorStateMatcher: ErrorStateMatcher,
4350
@Optional() @Self() ngControl: NgControl,
4451
@Optional() parentForm: NgForm,
4552
@Optional() parentFormGroup: FormGroupDirective,
53+
@Optional() formControlDirective: FormControlDirective,
4654
) {
4755
super(
4856
elementRef,
@@ -52,11 +60,27 @@ export class RangeInputComponent extends CustomFormControlDirective<NumericRange
5260
parentFormGroup,
5361
);
5462

63+
this.formControlDirective = formControlDirective;
5564
this.formGroup.valueChanges.subscribe((value) => {
5665
this.value = value;
57-
console.log("internal value changes", this.value);
5866
});
5967
}
68+
69+
private validatorFunction: ValidatorFn = (): ValidationErrors | null => {
70+
if (this.value.from && this.value.to && this.value.from > this.value.to) {
71+
return {
72+
fromGreaterThanTo: "The from value is greater than the to value.",
73+
};
74+
} else {
75+
return null;
76+
}
77+
};
78+
79+
ngAfterViewInit() {
80+
if (!this.deactivateValidation) {
81+
this.formControlDirective.form.addValidators([this.validatorFunction]);
82+
}
83+
}
6084
}
6185

6286
export class NumericRange {

0 commit comments

Comments
 (0)