Skip to content

Commit a9798d2

Browse files
committed
fix(file): fix multiple file fields
when a form contains several file fields only the first file was taken into account fix #937 Signed-off-by: btry <tbugier@teclib.com>
1 parent 4ee6173 commit a9798d2

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

inc/fields/filefield.class.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,21 @@ public function displayField($canEdit = true) {
5757

5858
public function isValid($value) {
5959
// If the field is required it can't be empty
60-
if ($this->isRequired() && (empty($_POST['_formcreator_field_' . $this->fields['id']][0])
61-
|| !is_file(GLPI_TMP_DIR . '/' . $_POST['_formcreator_field_' . $this->fields['id']][0]))) {
62-
Session::addMessageAfterRedirect(__('A required file is missing:', 'formcreator') . ' ' . $this->fields['name'], false, ERROR);
63-
return false;
60+
61+
if (!$this->isRequired()) {
62+
return true;
63+
}
64+
65+
if (is_array($_POST['_formcreator_field_' . $this->fields['id']])
66+
&& count($_POST['_formcreator_field_' . $this->fields['id']]) === 1) {
67+
$file = current($_POST['_formcreator_field_' . $this->fields['id']]);
68+
if (is_file(GLPI_TMP_DIR . '/' . $file)) {
69+
return true;
70+
}
6471
}
6572

66-
// All is OK
67-
return true;
73+
Session::addMessageAfterRedirect(__('A required file is missing:', 'formcreator') . ' ' . $this->fields['name'], false, ERROR);
74+
return false;
6875
}
6976

7077
public static function getName() {

inc/form_answer.class.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -903,9 +903,12 @@ private function transformAnswerValue(PluginFormcreatorQuestion $question, $valu
903903
} else {
904904
$answer_value = '';
905905
}
906-
} else if ((isset($_POST['_formcreator_field_' . $question->getID()]['0']))
907-
&& (is_file(GLPI_TMP_DIR . '/' . $_POST['_formcreator_field_' . $question->getID()]['0']))) {
908-
$answer_value = $this->saveDocument($form, $question, $_POST['_formcreator_field_' . $question->getID()]['0']);
906+
} else if (is_array($_POST['_formcreator_field_' . $question->getID()])
907+
&& count($_POST['_formcreator_field_' . $question->getID()]) === 1) {
908+
$file = current($_POST['_formcreator_field_' . $question->getID()]);
909+
if (is_file(GLPI_TMP_DIR . '/' . $file)) {
910+
$answer_value = $this->saveDocument($form, $question, $file);
911+
}
909912
}
910913

911914
return $answer_value;

0 commit comments

Comments
 (0)