]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/shared-forms/form-reactive.ts
Enhance plugin video fields
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-forms / form-reactive.ts
index adf6cb8941dff297f0ff394f9e255d495bf51349..30b59c141d29f867a6d1f417f61f58993116b488 100644 (file)
@@ -51,18 +51,23 @@ export abstract class FormReactive {
       }
 
       // clear previous error message (if any)
-      formErrors[ field ] = ''
+      formErrors[field] = ''
       const control = form.get(field)
 
       if (control.dirty) this.formChanged = true
 
-      // Don't care if dirty on force check
-      const isDirty = control.dirty || forceCheck === true
-      if (control && isDirty && control.enabled && !control.valid) {
-        const messages = validationMessages[ field ]
-        for (const key of Object.keys(control.errors)) {
-          formErrors[ field ] += messages[ key ] + ' '
-        }
+      if (forceCheck) control.updateValueAndValidity({ emitEvent: false })
+      if (!control || !control.dirty || !control.enabled || control.valid) continue
+
+      const staticMessages = validationMessages[field]
+      for (const key of Object.keys(control.errors)) {
+        const formErrorValue = control.errors[key]
+
+        // Try to find error message in static validation messages first
+        // Then check if the validator returns a string that is the error
+        if (typeof formErrorValue === 'boolean') formErrors[field] += staticMessages[key] + ' '
+        else if (typeof formErrorValue === 'string') formErrors[field] += control.errors[key]
+        else throw new Error('Form error value of ' + field + ' is invalid')
       }
     }
   }