Skip to content

Commit

Permalink
fix(file): fix multiple file fields
Browse files Browse the repository at this point in the history
when a form contains several file fields only the first file was taken into account

fix #937

Signed-off-by: btry <tbugier@teclib.com>
  • Loading branch information
btry committed Apr 25, 2018
1 parent 4ee6173 commit a9798d2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
19 changes: 13 additions & 6 deletions inc/fields/filefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,21 @@ public function displayField($canEdit = true) {

public function isValid($value) {
// If the field is required it can't be empty
if ($this->isRequired() && (empty($_POST['_formcreator_field_' . $this->fields['id']][0])
|| !is_file(GLPI_TMP_DIR . '/' . $_POST['_formcreator_field_' . $this->fields['id']][0]))) {
Session::addMessageAfterRedirect(__('A required file is missing:', 'formcreator') . ' ' . $this->fields['name'], false, ERROR);
return false;

if (!$this->isRequired()) {
return true;
}

if (is_array($_POST['_formcreator_field_' . $this->fields['id']])
&& count($_POST['_formcreator_field_' . $this->fields['id']]) === 1) {
$file = current($_POST['_formcreator_field_' . $this->fields['id']]);
if (is_file(GLPI_TMP_DIR . '/' . $file)) {
return true;
}
}

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

public static function getName() {
Expand Down
9 changes: 6 additions & 3 deletions inc/form_answer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -903,9 +903,12 @@ private function transformAnswerValue(PluginFormcreatorQuestion $question, $valu
} else {
$answer_value = '';
}
} else if ((isset($_POST['_formcreator_field_' . $question->getID()]['0']))
&& (is_file(GLPI_TMP_DIR . '/' . $_POST['_formcreator_field_' . $question->getID()]['0']))) {
$answer_value = $this->saveDocument($form, $question, $_POST['_formcreator_field_' . $question->getID()]['0']);
} else if (is_array($_POST['_formcreator_field_' . $question->getID()])
&& count($_POST['_formcreator_field_' . $question->getID()]) === 1) {
$file = current($_POST['_formcreator_field_' . $question->getID()]);
if (is_file(GLPI_TMP_DIR . '/' . $file)) {
$answer_value = $this->saveDocument($form, $question, $file);
}
}

return $answer_value;
Expand Down

0 comments on commit a9798d2

Please sign in to comment.