]>
Commit | Line | Data |
---|---|---|
830b4faf | 1 | import { I18n } from '@ngx-translate/i18n-polyfill' |
978c9d49 | 2 | import { AbstractControl, FormControl, Validators } from '@angular/forms' |
830b4faf C |
3 | import { Injectable } from '@angular/core' |
4 | import { BuildFormValidator } from '@app/shared' | |
978c9d49 | 5 | import { VideoPlaylistPrivacy } from '@shared/models' |
830b4faf C |
6 | |
7 | @Injectable() | |
8 | export class VideoPlaylistValidatorsService { | |
9 | readonly VIDEO_PLAYLIST_DISPLAY_NAME: BuildFormValidator | |
10 | readonly VIDEO_PLAYLIST_PRIVACY: BuildFormValidator | |
11 | readonly VIDEO_PLAYLIST_DESCRIPTION: BuildFormValidator | |
12 | readonly VIDEO_PLAYLIST_CHANNEL_ID: BuildFormValidator | |
13 | ||
14 | constructor (private i18n: I18n) { | |
15 | this.VIDEO_PLAYLIST_DISPLAY_NAME = { | |
16 | VALIDATORS: [ | |
17 | Validators.required, | |
18 | Validators.minLength(1), | |
19 | Validators.maxLength(120) | |
20 | ], | |
21 | MESSAGES: { | |
22 | 'required': this.i18n('Display name is required.'), | |
23 | 'minlength': this.i18n('Display name must be at least 1 character long.'), | |
24 | 'maxlength': this.i18n('Display name cannot be more than 120 characters long.') | |
25 | } | |
26 | } | |
27 | ||
28 | this.VIDEO_PLAYLIST_PRIVACY = { | |
29 | VALIDATORS: [ | |
30 | Validators.required | |
31 | ], | |
32 | MESSAGES: { | |
33 | 'required': this.i18n('Privacy is required.') | |
34 | } | |
35 | } | |
36 | ||
37 | this.VIDEO_PLAYLIST_DESCRIPTION = { | |
38 | VALIDATORS: [ | |
39 | Validators.minLength(3), | |
40 | Validators.maxLength(1000) | |
41 | ], | |
42 | MESSAGES: { | |
43 | 'minlength': i18n('Description must be at least 3 characters long.'), | |
44 | 'maxlength': i18n('Description cannot be more than 1000 characters long.') | |
45 | } | |
46 | } | |
47 | ||
48 | this.VIDEO_PLAYLIST_CHANNEL_ID = { | |
49 | VALIDATORS: [ ], | |
978c9d49 C |
50 | MESSAGES: { |
51 | 'required': this.i18n('The channel is required when the playlist is public.') | |
52 | } | |
53 | } | |
54 | } | |
55 | ||
56 | setChannelValidator (channelControl: AbstractControl, privacy: VideoPlaylistPrivacy) { | |
57 | if (privacy.toString() === VideoPlaylistPrivacy.PUBLIC.toString()) { | |
58 | channelControl.setValidators([ Validators.required ]) | |
59 | } else { | |
60 | channelControl.setValidators(null) | |
830b4faf | 61 | } |
978c9d49 C |
62 | |
63 | channelControl.markAsDirty() | |
64 | channelControl.updateValueAndValidity() | |
830b4faf C |
65 | } |
66 | } |