Skip to content

Commit

Permalink
fix(filefield): mandatory fails when file is uploaded
Browse files Browse the repository at this point in the history
also fix display of files in formansxer

Signed-off-by: Thierry Bugier <[email protected]>
  • Loading branch information
btry committed Jan 28, 2021
1 parent ac9f693 commit 58c2dd1
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions inc/field/filefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ public function isValid(): bool {
}

// If the field is required it can't be empty
if (($this->isRequired() && count($this->uploadData) < 1)) {
$key = '_formcreator_field_' . $this->question->getID();
if (($this->isRequired() && count($this->uploads[$key]) < 1)) {
Session::addMessageAfterRedirect(
sprintf(__('A required file is missing: %s', 'formcreator'), $this->getLabel()),
false,
Expand Down Expand Up @@ -179,7 +180,11 @@ public function saveUploads($input) {
}

public function hasInput($input): bool {
return isset($input['_formcreator_field_' . $this->question->getID()]);
// key with unserscore when testing unput from a requester
// key without underscore when testing data from DB (display a saved answer)
$key = 'formcreator_field_' . $this->question->getID();
return isset($input["_$key"])
|| isset($input[$key]);
}

/**
Expand Down Expand Up @@ -254,6 +259,13 @@ public function parseAnswerValues($input, $nonDestructive = false): bool {
}
return true;
}
if (isset($input[$key])) {
// To restore input from database
$this->uploadData = json_decode($input[$key]);
$this->value = __('Attached document', 'formcreator');
return true;

}
$this->uploadData = [];
$this->value = '';
return true;
Expand Down

0 comments on commit 58c2dd1

Please sign in to comment.