Skip to content

Commit 6988ba1

Browse files
committed
add link validation for an achievement
1 parent 312b65c commit 6988ba1

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/app/users/users-achievements/users-achievements-update.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export class UsersAchievementsUpdateComponent implements OnInit, OnDestroy {
133133
...achievement,
134134
title: [ achievement.title, CustomValidators.required ],
135135
description: [ achievement.description ],
136-
link: [ achievement.link ],
136+
link: [ achievement.link, [], CustomValidators.validLink ],
137137
date: [ achievement.date, null, ac => this.validatorService.notDateInFuture$(ac) ]
138138
}),
139139
{ onSubmit: (formValue, formGroup) => {

src/app/validators/custom-validators.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,15 @@ export class CustomValidators {
246246

247247
static validLink(ac: AbstractControl): Promise<ValidationErrors | null> | Observable<ValidationErrors | null> {
248248
return new Promise((resolve, reject) => {
249-
try {
250-
const url = new URL(ac.value);
249+
if (!ac.value) {
251250
resolve(null);
252-
} catch (_) {
253-
resolve({ 'invalidLink': true });
251+
} else {
252+
try {
253+
const url = new URL(ac.value);
254+
resolve(null);
255+
} catch (_) {
256+
resolve({ 'invalidLink': true });
257+
}
254258
}
255259
});
256260
}

0 commit comments

Comments
 (0)