]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/+videos/+video-edit/shared/video-edit.component.ts
Handle async validators
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-edit / shared / video-edit.component.ts
index 8ce36121d0b65408a67cd57975308afa9de080ee..be3bbe9be844768427bd2415672de949ff3c7b78 100644 (file)
@@ -2,7 +2,7 @@ import { forkJoin } from 'rxjs'
 import { map } from 'rxjs/operators'
 import { SelectChannelItem } from 'src/types/select-options-item.model'
 import { ChangeDetectorRef, Component, EventEmitter, Input, NgZone, OnDestroy, OnInit, Output, ViewChild } from '@angular/core'
-import { AbstractControl, FormArray, FormControl, FormGroup, ValidationErrors, Validators } from '@angular/forms'
+import { AbstractControl, FormArray, FormControl, FormGroup, Validators } from '@angular/forms'
 import { HooksService, PluginService, ServerService } from '@app/core'
 import { removeElementFromArray } from '@app/helpers'
 import { BuildFormValidator } from '@app/shared/form-validators'
@@ -309,10 +309,10 @@ export class VideoEditComponent implements OnInit, OnDestroy {
     for (const setting of this.pluginFields) {
       await this.pluginService.translateSetting(setting.pluginInfo.plugin.npmName, setting.commonOptions)
 
-      const validator = (control: AbstractControl): ValidationErrors | null => {
+      const validator = async (control: AbstractControl) => {
         if (!setting.commonOptions.error) return null
 
-        const error = setting.commonOptions.error({ formValues: this.form.value, value: control.value })
+        const error = await setting.commonOptions.error({ formValues: this.form.value, value: control.value })
 
         return error?.error ? { [setting.commonOptions.name]: error.text } : null
       }
@@ -320,7 +320,8 @@ export class VideoEditComponent implements OnInit, OnDestroy {
       const name = setting.commonOptions.name
 
       pluginObj[name] = {
-        VALIDATORS: [ validator ],
+        ASYNC_VALIDATORS: [ validator ],
+        VALIDATORS: [],
         MESSAGES: {}
       }
 
@@ -342,6 +343,9 @@ export class VideoEditComponent implements OnInit, OnDestroy {
 
     this.cd.detectChanges()
     this.pluginFieldsAdded.emit()
+
+    // Plugins may need other control values to calculate potential errors
+    this.form.valueChanges.subscribe(() => this.formValidatorService.updateTreeValidity(this.pluginDataFormGroup))
   }
 
   private trackPrivacyChange () {