Skip to content

Commit

Permalink
Add whitespace validation (#1237)
Browse files Browse the repository at this point in the history
* Add whitespace validation

* Update whitespace validation for new issue

* Update whitespace validation for title of new issues

* Update whitespace validation for title of new issues

* Move validators into core

* Update import order

---------

Co-authored-by: Misra Aditya <[email protected]>
  • Loading branch information
MadLamprey and Misra Aditya authored Jan 25, 2024
1 parent b20b3ef commit fe7be44
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/app/core/validators/noWhitespace.validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { AbstractControl, ValidationErrors, ValidatorFn } from '@angular/forms';

export function noWhitespace(): ValidatorFn {
return (title: AbstractControl): ValidationErrors | null => {
if (title.value && title.value.trim() === '') {
return { whitespace: true };
}
return null;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ <h1 class="mat-display-1 title">New Issue</h1>
<mat-form-field>
<input id="title" formControlName="title" matInput placeholder="Title" required maxlength="256" />
<mat-error *ngIf="title.errors && title.errors['required'] && (title.touched || title.dirty)"> Title required. </mat-error>
<mat-error *ngIf="title.errors && title.errors['whitespace']"> Title cannot contain only whitespaces. </mat-error>
<mat-error *ngIf="title.errors && title.errors['maxlength']"> Title cannot exceed 256 characters. </mat-error>
<mat-hint *ngIf="title.value?.length >= 206"> {{ 256 - title.value?.length }} characters remaining. </mat-hint>
</mat-form-field>
Expand Down
4 changes: 3 additions & 1 deletion src/app/phase-bug-reporting/new-issue/new-issue.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Issue } from '../../core/models/issue.model';
import { ErrorHandlingService } from '../../core/services/error-handling.service';
import { IssueService } from '../../core/services/issue.service';
import { LabelService } from '../../core/services/label.service';
import { noWhitespace } from '../../core/validators/noWhitespace.validator';
import { SUBMIT_BUTTON_TEXT } from '../../shared/view-issue/view-issue.component';

@Component({
Expand All @@ -28,7 +29,7 @@ export class NewIssueComponent implements OnInit {

ngOnInit() {
this.newIssueForm = this.formBuilder.group({
title: ['', [Validators.required, Validators.maxLength(256)]],
title: ['', [Validators.required, Validators.maxLength(256), noWhitespace()]],
description: [''],
severity: ['', Validators.required],
type: ['', Validators.required]
Expand All @@ -41,6 +42,7 @@ export class NewIssueComponent implements OnInit {
if (this.newIssueForm.invalid) {
return;
}

this.isFormPending = true;
this.issueService
.createIssue(this.title.value, Issue.updateDescription(this.description.value), this.severity.value, this.type.value)
Expand Down

0 comments on commit fe7be44

Please sign in to comment.