Skip to content

Commit 22805a3

Browse files
author
wg102
committed
fix: show error when element is untouched
1 parent f2614b6 commit 22805a3

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

frontend/src/app/resource-form/volume/volume.component.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
matInput
1919
formControlName="name"
2020
*ngIf="volume.value.type == 'New'; else existingPvcControl"
21+
[errorStateMatcher]="matcher"
2122
/>
2223
<ng-template #existingPvcControl>
23-
<mat-select formControlName="name">
24+
<mat-select formControlName="name" [errorStateMatcher]="matcher">
2425
<mat-option *ngFor="let pvc of existingPVCs" [value]="pvc">{{
2526
pvc
2627
}}</mat-option>

frontend/src/app/resource-form/volume/volume.component.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Component, OnInit, Input, OnDestroy } from "@angular/core";
2-
import { FormGroup, Validators, ValidatorFn, AbstractControl } from "@angular/forms";
2+
import { FormGroup, Validators, ValidatorFn, AbstractControl, FormControl, FormGroupDirective, NgForm } from "@angular/forms";
33
import { Volume } from "src/app/utils/types";
44
import { Subscription } from "rxjs";
55
import { TranslateService } from "@ngx-translate/core";
66
import { NamespaceService } from "src/app/services/namespace.service";
77
import { KubernetesService } from "src/app/services/kubernetes.service";
8+
import {ErrorStateMatcher} from '@angular/material/core';
89

910
@Component({
1011
selector: "app-volume",
@@ -18,6 +19,8 @@ export class VolumeComponent implements OnInit, OnDestroy {
1819

1920
currentPVC: Volume;
2021
existingPVCs: Set<string> = new Set();
22+
// Specific error matcher for volume name field
23+
matcher = new PvcErrorStateMatcher();
2124

2225
subscriptions = new Subscription();
2326

@@ -188,7 +191,6 @@ export class VolumeComponent implements OnInit, OnDestroy {
188191

189192
showNameError() {
190193
const volumeName = this.volume.get("name");
191-
192194
if (volumeName.hasError("required")) {
193195
return this.translate.instant("volume.errorNameRequired");
194196
}
@@ -208,3 +210,11 @@ export class VolumeComponent implements OnInit, OnDestroy {
208210
};
209211
}
210212
}
213+
// Error when invalid control is dirty, touched, or submitted
214+
export class PvcErrorStateMatcher implements ErrorStateMatcher {
215+
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
216+
const isSubmitted = form && form.submitted;
217+
//Allows to control when volume is untouched but already assigned
218+
return !!(control && control.invalid && (control.dirty || control.touched || isSubmitted || !control.hasError("pattern")));
219+
}
220+
}

frontend/src/assets/i18n/fr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
"lblName": "Nom",
128128
"errorNameRequired": "Le nom du volume ne peut pas être vide",
129129
"errorNamePattern": "Le nom du volume ne peut contenir que des caractères alphanumériques en minuscule, '-' ou '.', et doit commencer et se terminer par un caractères alphanumériques",
130-
"errorMountedVolume":"Le volume est déjà monté sur un autre bloc note et ne peut être sélectionné actuellement",
130+
"errorMountedVolume":"Le volume est déjà monté sur un autre bloc-notes et ne peut être sélectionné actuellement",
131131
"lblSize": "Taille",
132132
"lblMode": "Mode",
133133
"optReadWriteOnce": "ReadWriteOnce",

0 commit comments

Comments
 (0)