Skip to content

Commit 647577a

Browse files
committed
fix validators for links in project & checking for empty values for link field
1 parent dd69c80 commit 647577a

4 files changed

Lines changed: 34 additions & 16 deletions

File tree

projects/social_platform/src/app/office/projects/edit/services/project-form.service.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { ActivatedRoute } from "@angular/router";
1313
import { PartnerProgramFields } from "@office/models/partner-program-fields.model";
1414
import { Project } from "@office/models/project.model";
1515
import { ProjectService } from "@office/services/project.service";
16+
import { optionalUrlOrMentionValidator } from "@utils/optionalUrl.validator";
1617
import { stripNullish } from "@utils/stripNull";
1718
import { concatMap, filter } from "rxjs";
1819
/**
@@ -45,7 +46,7 @@ export class ProjectFormService {
4546
implementationDeadline: [null],
4647
trl: [null],
4748
links: this.fb.array([]),
48-
link: ["", Validators.pattern(/^(https?:\/\/)/)],
49+
link: ["", optionalUrlOrMentionValidator],
4950
industryId: [undefined],
5051
description: ["", [Validators.maxLength(800)]],
5152
presentationAddress: [""],
@@ -130,7 +131,7 @@ export class ProjectFormService {
130131
}
131132

132133
links.forEach(link => {
133-
linksFormArray.push(this.fb.control(link, [Validators.required]));
134+
linksFormArray.push(this.fb.control(link, optionalUrlOrMentionValidator));
134135
});
135136
}
136137

@@ -193,7 +194,13 @@ export class ProjectFormService {
193194
* @returns объект значений формы без nullish
194195
*/
195196
public getFormValue(): any {
196-
return stripNullish(this.projectForm.value);
197+
const value = stripNullish(this.projectForm.value);
198+
199+
if (Array.isArray(value["links"])) {
200+
value["links"] = value["links"].map((v: string) => v?.trim()).filter((v: string) => !!v);
201+
}
202+
203+
return value;
197204
}
198205

199206
// Геттеры для быстрого доступа к контролам основной формы

projects/social_platform/src/app/office/projects/edit/shared/project-main-step/project-main-step.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@
415415
<div formArrayName="links">
416416
@if (hasLinks) {
417417
<ul class="project__links--wrapper">
418-
@for (control of linksArray.controls; track trackByIndex($index)) {
418+
@for (control of links.controls; track trackByIndex($index)) {
419419
<li class="project__links">
420420
<fieldset style="flex-grow: 1">
421421
<label class="text-body-12 field-label">ссылка на контакты и сообщества</label>

projects/social_platform/src/app/office/projects/edit/shared/project-main-step/project-main-step.component.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { AvatarComponent } from "@ui/components/avatar/avatar.component";
3030
import { ProjectService } from "@office/services/project.service";
3131
import { RouterLink } from "@angular/router";
3232
import { generateOptionsList } from "@utils/generate-options-list";
33+
import { optionalUrlOrMentionValidator } from "@utils/optionalUrl.validator";
3334

3435
@Component({
3536
selector: "app-project-main-step",
@@ -154,11 +155,7 @@ export class ProjectMainStepComponent implements OnInit, OnDestroy {
154155
}
155156

156157
get links(): FormArray {
157-
return this.projectContactsService.links;
158-
}
159-
160-
get linksItems() {
161-
return this.projectContactsService.linksItems;
158+
return this.projectForm.get("links") as FormArray;
162159
}
163160

164161
// Геттеры для работы с целями
@@ -194,7 +191,7 @@ export class ProjectMainStepComponent implements OnInit, OnDestroy {
194191
* Проверяет, есть ли ссылки для отображения
195192
*/
196193
get hasLinks(): boolean {
197-
return this.linksArray.length > 0;
194+
return this.links.length > 0;
198195
}
199196

200197
/**
@@ -204,15 +201,11 @@ export class ProjectMainStepComponent implements OnInit, OnDestroy {
204201
return this.goals.length > 0;
205202
}
206203

207-
get linksArray(): FormArray {
208-
return this.projectForm.get("links") as FormArray;
209-
}
210-
211204
/**
212205
* Добавление ссылки
213206
*/
214207
addLink(): void {
215-
this.linksArray.push(this.fb.control("", Validators.required));
208+
this.links.push(this.fb.control("", optionalUrlOrMentionValidator));
216209
}
217210

218211
/**
@@ -228,7 +221,7 @@ export class ProjectMainStepComponent implements OnInit, OnDestroy {
228221
* @param index - индекс ссылки
229222
*/
230223
removeLink(index: number): void {
231-
this.linksArray.removeAt(index);
224+
this.links.removeAt(index);
232225
}
233226

234227
/**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/** @format */
2+
3+
import { AbstractControl, ValidationErrors } from "@angular/forms";
4+
5+
export const optionalUrlOrMentionValidator = (
6+
control: AbstractControl
7+
): ValidationErrors | null => {
8+
const value: string = control.value;
9+
10+
if (!value.trim()) {
11+
return null;
12+
}
13+
14+
const isUrl = /^https?:\/\/.+$/.test(value);
15+
const isMention = /^@[a-zA-Z0-9_]+$/.test(value);
16+
17+
return isUrl || isMention ? null : { invalidLink: true };
18+
};

0 commit comments

Comments
 (0)