add validation for file fields in formbuilder
authorKurund Jalmi <kurundjalmi@thirdsectordesign.org>
Fri, 24 Feb 2023 19:52:26 +0000 (19:52 +0000)
committerKurund Jalmi <kurundjalmi@thirdsectordesign.org>
Fri, 24 Feb 2023 19:52:26 +0000 (19:52 +0000)
ext/afform/core/Civi/Api4/Action/Afform/Submit.php
ext/afform/core/ang/af/afForm.component.js

index 83bdf2d759cf35d24b4999721e869ce810316de7..1e886b126aeac2bf2a481eddf2f0230c89e057b6 100644 (file)
@@ -202,6 +202,12 @@ class Submit extends AbstractProcessor {
       return NULL;
     }
     $fullDefn = FormDataModel::getField($apiEntity, $fieldName, 'create');
+
+    // we don't need to validate the file fields as it's handled separately
+    if ($fullDefn['input_type'] === 'File') {
+      return NULL;
+    }
+
     $isRequired = $attributes['defn']['required'] ?? $fullDefn['required'] ?? FALSE;
     if ($isRequired) {
       $label = $attributes['defn']['label'] ?? $fullDefn['label'];
index e13ea5dae67f854ce02c27d84d693edb78523952..9951e5ddbdc8706c7bbe9158a63d42242dba6921 100644 (file)
         return str;
       }
 
+      function validateFileFields() {
+        var valid = true;
+        $("af-form[ng-form=" + ctrl.getFormMeta().name +"] input[type='file']").each((index, fld) => {
+          if ($(fld).attr('required') && $(fld).get(0).files.length == 0) {
+            valid = false;
+          }
+        });
+        return valid;
+      }
+
       this.submit = function() {
-        if (!ctrl.ngForm.$valid) {
+        // validate required fields on the form
+        if (!ctrl.ngForm.$valid || !validateFileFields()) {
           CRM.alert(ts('Please fill all required fields.'), ts('Form Error'));
           return;
         }