Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lectures: Disable submit button on invalid form and add tooltip #9846

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="col-12">
<form [formGroup]="form" (ngSubmit)="submitForm()">
<div class="form-group">
<label for="name">{{ 'artemisApp.attachmentUnit.createAttachmentUnit.name' | artemisTranslate }} *</label>
<label for="name">{{ 'artemisApp.attachmentUnit.createAttachmentUnit.name' | artemisTranslate }}*</label>
<input
type="text"
class="form-control"
Expand Down Expand Up @@ -63,7 +63,6 @@
@if (isFileTooBig()) {
<div class="alert alert-danger">
{{ 'artemisApp.attachmentUnit.createAttachmentUnit.fileTooBig' | artemisTranslate }}
{{ 'artemisApp.attachmentUnit.createAttachmentUnit.fileLimitation' | artemisTranslate }}
</div>
}
@if (!fileName() && fileInputTouched) {
Expand Down Expand Up @@ -105,14 +104,16 @@
</div>
<div class="row">
<div class="col-12">
<button id="submitButton" class="btn btn-primary" type="submit" [disabled]="!isFormValid()">
<span jhiTranslate="entity.action.submit"></span>
</button>
@if (hasCancelButton()) {
<button type="button" (click)="cancelForm()" class="btn btn-default">
<fa-icon [icon]="faTimes" />&nbsp;<span jhiTranslate="entity.action.cancel"></span>
<span [ngbTooltip]="tooltipText" container="body" class="d-inline-block">
<button id="submitButton" class="btn btn-primary" type="submit" [disabled]="!isFormValid()">
<span jhiTranslate="entity.action.submit"></span>
</button>
}
@if (hasCancelButton()) {
<button type="button" (click)="cancelForm()" class="btn btn-default">
<fa-icon [icon]="faTimes" />&nbsp;<span jhiTranslate="entity.action.cancel"></span>
</button>
}
</span>
</div>
</div>
</form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ACCEPTED_FILE_EXTENSIONS_FILE_BROWSER, ALLOWED_FILE_EXTENSIONS_HUMAN_RE
import { CompetencyLectureUnitLink } from 'app/entities/competency.model';
import { MAX_FILE_SIZE } from 'app/shared/constants/input.constants';
import { toSignal } from '@angular/core/rxjs-interop';
import { TranslateService } from '@ngx-translate/core';

export interface AttachmentUnitFormData {
formProperties: FormProperties;
Expand Down Expand Up @@ -56,6 +57,7 @@ export class AttachmentUnitFormComponent implements OnChanges {
fileName = signal<string | undefined>(undefined);
isFileTooBig = signal<boolean>(false);

private readonly translateService = inject(TranslateService);
private readonly formBuilder = inject(FormBuilder);
form: FormGroup = this.formBuilder.group({
name: [undefined as string | undefined, [Validators.required, Validators.maxLength(255)]],
Expand All @@ -68,7 +70,7 @@ export class AttachmentUnitFormComponent implements OnChanges {
private readonly statusChanges = toSignal(this.form.statusChanges ?? 'INVALID');

isFormValid = computed(() => {
return (this.statusChanges() === 'VALID' || this.fileName()) && !this.isFileTooBig();
return this.statusChanges() === 'VALID' && !this.isFileTooBig() && this.nameControl?.value != '' && this.fileName();
});

ngOnChanges(): void {
Expand All @@ -94,6 +96,23 @@ export class AttachmentUnitFormComponent implements OnChanges {
this.isFileTooBig.set(this.file.size > MAX_FILE_SIZE);
}

get tooltipText(): string | null {
if (!this.fileInputTouched && this.nameControl?.invalid) {
return (
this.translateService.instant('artemisApp.attachmentUnit.createAttachmentUnit.nameRequiredValidationError') +
' ' +
this.translateService.instant('artemisApp.attachmentUnit.createAttachmentUnit.fileRequiredValidationError')
);
}
if (!this.fileInputTouched) {
return this.translateService.instant('artemisApp.attachmentUnit.createAttachmentUnit.fileRequiredValidationError');
}
if (this.nameControl?.invalid) {
return this.translateService.instant('artemisApp.attachmentUnit.createAttachmentUnit.nameRequiredValidationError');
}
return null;
}
laxerhd marked this conversation as resolved.
Show resolved Hide resolved

get nameControl() {
return this.form.get('name');
}
Expand Down