Skip to content

Commit 9e8fc42

Browse files
committed
Update form errors to use the new Angular @if syntax
1 parent efc6e14 commit 9e8fc42

File tree

2 files changed

+44
-26
lines changed

2 files changed

+44
-26
lines changed

frontend/src/app/questions/questions.component.html

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ <h3 class="m-0">Manage Questions</h3>
8686
<div class="field grid col-12">
8787
<label for="title">Title</label>
8888
<input
89-
formControlName="textTitle"
89+
formControlName="title"
9090
type="text"
9191
pInputText
9292
id="title"
9393
required
9494
class="text-base text-color surface-overlay p-2 border-1 border-solid surface-border border-round appearance-none outline-none focus:border-primary w-full" />
95-
<small class="p-error" *ngIf="submitted && (question.title === undefined || !question.title)">
96-
Title is required.
97-
</small>
95+
@if (isTitleInvalid) {
96+
<small class="text-red-300">Title is required.</small>
97+
}
9898
</div>
9999
<div class="formgrid grid field">
100100
<div class="field col-12 md:col-6">
@@ -107,11 +107,9 @@ <h3 class="m-0">Manage Questions</h3>
107107
optionLabel="label"
108108
optionValue="value"
109109
placeholder="Select Topics" />
110-
<small
111-
class="p-error"
112-
*ngIf="submitted && (question.topics === undefined || question.topics.length === 0)">
113-
Topic is required.
114-
</small>
110+
@if (isTopicsInvalid) {
111+
<small class="text-red-300">Topic(s) is required.</small>
112+
}
115113
</div>
116114
<div class="field col-12 md:col-6">
117115
<label for="questionTopics">Difficulty</label>
@@ -123,34 +121,34 @@ <h3 class="m-0">Manage Questions</h3>
123121
optionLabel="label"
124122
optionValue="value"
125123
placeholder="Select Difficulty" />
126-
<small
127-
class="p-error"
128-
*ngIf="submitted && (question.difficulty === undefined || !question.difficulty)">
129-
Difficulty is required.
130-
</small>
124+
@if (isDifficultyInvalid) {
125+
<small class="text-red-300">Difficulty is required.</small>
126+
}
131127
</div>
132128
</div>
133129
<div class="field grid col-12" field>
134130
<label for="questionDescription">Description</label>
135131
<textarea
136-
formControlName="textDescription"
132+
formControlName="description"
137133
[required]="true"
138134
id="questionDescription"
139135
type="text"
140136
rows="6"
141137
class="text-base text-color surface-overlay p-2 border-1 border-solid surface-border border-round appearance-none outline-none focus:border-primary w-full">
142138
</textarea>
143-
<small
144-
class="p-error"
145-
*ngIf="submitted && (question.description === undefined || !question.description)">
146-
Description is required.
147-
</small>
139+
@if (isDescriptionInvalid) {
140+
<small class="text-red-300">Description is required.</small>
141+
}
148142
</div>
149143
</form>
150144

151145
<ng-template pTemplate="footer">
152146
<p-button label="Cancel" [text]="true" severity="secondary" (onClick)="isDialogVisible = false" />
153-
<p-button label="Save" class="p-button-success" (onClick)="saveQuestion()" />
147+
<p-button
148+
label="Save"
149+
class="p-button-success"
150+
(onClick)="saveQuestion()"
151+
[disabled]="!questionFormGroup.valid" />
154152
</ng-template>
155153
</p-dialog>
156154

frontend/src/app/questions/questions.component.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, OnInit } from '@angular/core';
22
import { TableModule } from 'primeng/table';
3-
import { FormsModule } from '@angular/forms';
3+
import { FormsModule, Validators } from '@angular/forms';
44
import { ToastModule } from 'primeng/toast';
55
import { ToolbarModule } from 'primeng/toolbar';
66
import { ButtonModule } from 'primeng/button';
@@ -87,6 +87,26 @@ export class QuestionsComponent implements OnInit {
8787
this.initListeners();
8888
}
8989

90+
get isTitleInvalid(): boolean {
91+
const titleControl = this.questionFormGroup.controls['title'];
92+
return titleControl.dirty && titleControl.invalid;
93+
}
94+
95+
get isDescriptionInvalid(): boolean {
96+
const descriptionControl = this.questionFormGroup.controls['description'];
97+
return descriptionControl.dirty && descriptionControl.invalid;
98+
}
99+
100+
get isDifficultyInvalid(): boolean {
101+
const difficultyControl = this.questionFormGroup.controls['selectedDifficulty'];
102+
return difficultyControl.dirty && difficultyControl.invalid;
103+
}
104+
105+
get isTopicsInvalid(): boolean {
106+
const topicsControl = this.questionFormGroup.controls['selectedTopics'];
107+
return topicsControl.dirty && topicsControl.invalid;
108+
}
109+
90110
openNewQuestion() {
91111
this.resetFormGroup();
92112
this.question = {} as Question;
@@ -175,10 +195,10 @@ export class QuestionsComponent implements OnInit {
175195

176196
initFormGroup() {
177197
this.questionFormGroup = new FormGroup({
178-
selectedTopics: new FormControl<string[] | null>([]),
179-
selectedDifficulty: new FormControl<Difficulty[] | null>([]),
180-
textTitle: new FormControl<string | null>(''),
181-
textDescription: new FormControl<string | null>(''),
198+
selectedTopics: new FormControl<string[] | null>([], [Validators.required]),
199+
selectedDifficulty: new FormControl<Difficulty[] | null>([], [Validators.required]),
200+
title: new FormControl<string | null>('', [Validators.required]),
201+
description: new FormControl<string | null>('', [Validators.required]),
182202
});
183203
}
184204

0 commit comments

Comments
 (0)