From b652eb290abfda7162b2cf6ff887205abd1af32d Mon Sep 17 00:00:00 2001 From: Kurund Jalmi Date: Fri, 24 Feb 2023 19:52:26 +0000 Subject: [PATCH] add validation for file fields in formbuilder --- ext/afform/core/Civi/Api4/Action/Afform/Submit.php | 6 ++++++ ext/afform/core/ang/af/afForm.component.js | 13 ++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/ext/afform/core/Civi/Api4/Action/Afform/Submit.php b/ext/afform/core/Civi/Api4/Action/Afform/Submit.php index 83bdf2d759..1e886b126a 100644 --- a/ext/afform/core/Civi/Api4/Action/Afform/Submit.php +++ b/ext/afform/core/Civi/Api4/Action/Afform/Submit.php @@ -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']; diff --git a/ext/afform/core/ang/af/afForm.component.js b/ext/afform/core/ang/af/afForm.component.js index e13ea5dae6..9951e5ddbd 100644 --- a/ext/afform/core/ang/af/afForm.component.js +++ b/ext/afform/core/ang/af/afForm.component.js @@ -137,8 +137,19 @@ 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; } -- 2.25.1