Skip to content

Commit 572cfb7

Browse files
committed
fix: issue when saving multipart files
1 parent 4ffc651 commit 572cfb7

File tree

2 files changed

+89
-35
lines changed

2 files changed

+89
-35
lines changed

README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
[![Version](http://img.shields.io/packagist/v/fullscreeninteractive/silverstripe-manyfield.svg)](https://packagist.org/packages/fullscreeninteractive/silverstripe-manyfield)
44
[![License](http://img.shields.io/packagist/l/fullscreeninteractive/silverstripe-manyfield.svg)](license.md)
55

6-
A reusable approach to a form field which allows you to create and delete rows
7-
in Forms.
6+
A reusable approach to a form field which allows you to create and delete rows
7+
in either custom built forms or in Userforms.
88

9-
This is designed to work on the front-end with limited javascript (i.e it does
9+
This is designed to work on the front-end with limited javascript (i.e it does
1010
not require `GridField` or entwine).
1111

1212
Each row can relate to a DataObject subclass or simply to be used to capture the
@@ -35,7 +35,7 @@ $fields = new FieldList(
3535
);
3636
```
3737

38-
Data will either be saved as `setSwabList($data)`, `SwabList` database field or
38+
Data will either be saved as `setSwabList($data)`, `SwabList` database field or
3939
in the `SwabList` relation. If you are saving into a relation such as `HasMany`
4040
or `ManyMany` list then make sure you include a hidden field in your field list.
4141

@@ -69,13 +69,23 @@ Include a Hidden field `Sort` and make sure sorting is enabled.
6969

7070
## Javascript Events
7171

72-
If you have UI handlers that need to run when fields are added or removed
72+
If you have UI handlers that need to run when fields are added or removed
7373
(such as Date Pickers) create a handler on your `<body>` element and listen for
7474
either:
7575

7676
* `manyFieldAdded`
7777
* `manyFieldRemoved`
7878

79+
## FAQ
80+
81+
### When I use this with File fields I don't get files uploaded?
82+
83+
Make sure your form encoding is set to the correct MIME type.
84+
85+
```
86+
$form->setEncType(Form::ENC_TYPE_MULTIPART);
87+
```
88+
7989
## Licence
8090

81-
BSD 3-Clause License
91+
BSD 3-Clause License

src/EditableManyField.php

Lines changed: 73 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace FullscreenInteractive\ManyField;
44

55
use Dotenv\Exception\ValidationException;
6+
use Exception;
67
use SilverStripe\Forms\GridField\GridField;
78
use FullscreenInteractive\ManyField\ManyField;
9+
use Psr\Log\LoggerInterface;
810
use SilverStripe\Assets\Upload;
11+
use SilverStripe\Core\Injector\Injector;
912
use SilverStripe\Forms\FieldList;
1013
use SilverStripe\Forms\GridField\GridFieldButtonRow;
1114
use SilverStripe\Forms\GridField\GridFieldConfig;
@@ -156,9 +159,18 @@ public function getValueFromData($data)
156159

157160
foreach ($this->Children() as $field)
158161
{
159-
foreach ($incoming[$field->Name] as $i => $value) {
160-
if ($value && !empty($value)) {
161-
$rowHasValue[$i] = true;
162+
if (isset($incoming[$field->Name])) {
163+
foreach ($incoming[$field->Name] as $i => $value) {
164+
if ($value && !empty($value)) {
165+
$rowHasValue[$i] = true;
166+
}
167+
}
168+
} elseif (isset($incoming['name']) && isset($incoming['name'][$field->Name])) {
169+
// handle multi-part
170+
foreach ($incoming['name'][$field->Name] as $i => $value) {
171+
if ($value && !empty($value)) {
172+
$rowHasValue[$i] = true;
173+
}
162174
}
163175
}
164176
}
@@ -167,21 +179,37 @@ public function getValueFromData($data)
167179

168180
foreach ($this->Children() as $field)
169181
{
170-
foreach ($incoming[$field->Name] as $i => $value) {
171-
if (!isset($rowHasValue[$i])) {
172-
// empty row;
173-
continue;
174-
}
182+
if (isset($incoming[$field->Name])) {
183+
foreach ($incoming[$field->Name] as $i => $value) {
184+
if (!isset($rowHasValue[$i])) {
185+
// empty row;
186+
continue;
187+
}
188+
189+
if (!isset($rows[$i])) {
190+
$rows[$i] = [];
191+
}
175192

176-
if (!isset($rows[$i])) {
177-
$rows[$i] = [];
193+
$submittedField = $this->createNestedSubmittedFormField($field, [
194+
$field->Name => $value
195+
]);
196+
197+
$rows[$i][$field->Name] = $submittedField->ID;
178198
}
199+
} elseif (isset($incoming['name']) && isset($incoming['name'][$field->Name])) {
200+
// handle multi-part
201+
foreach ($incoming['name'][$field->Name] as $i => $value) {
202+
if (!isset($rowHasValue[$i])) {
203+
// empty row;
204+
continue;
205+
}
179206

180-
$submittedField = $this->createNestedSubmittedFormField($field, [
181-
$field->Name => $value
182-
]);
207+
$submittedField = $this->createNestedSubmittedFormField($field, [
208+
$field->Name => $value
209+
]);
183210

184-
$rows[$i][$field->Name] = $submittedField->ID;
211+
$rows[$i][$field->Name] = $submittedField->ID;
212+
}
185213
}
186214
}
187215

@@ -204,26 +232,42 @@ public function createNestedSubmittedFormField(EditableFormField $field, $data)
204232
}
205233
}
206234

207-
if (!empty($data[$field->Name])) {
208-
if (in_array(EditableFileField::class, $field->getClassAncestry())) {
209-
if (!empty($_FILES[$field->Name]['name'])) {
210-
$foldername = $field->getFormField()->getFolderName();
211-
$upload = Upload::create();
235+
$file = null;
236+
if (!empty($_FILES[$field->Name]['name'])) {
237+
$file = $_FILES[$field->Name];
238+
} else if (!empty($_FILES[$this->Name]['name'])) {
239+
if (!empty($_FILES[$this->Name]['name'][$field->Name])) {
240+
$file = [
241+
'tmp_name' => $_FILES[$this->Name]['tmp_name'][$field->Name][0],
242+
'type' => $_FILES[$this->Name]['type'][$field->Name][0],
243+
'name' => $_FILES[$this->Name]['name'][$field->Name][0],
244+
'error' => $_FILES[$this->Name]['error'][$field->Name][0],
245+
'size' => $_FILES[$this->Name]['size'][$field->Name][0]
246+
];
247+
}
248+
}
212249

213-
try {
214-
$upload->loadIntoFile($_FILES[$field->Name], null, $foldername);
250+
if ($file) {
251+
$foldername = $field->getFormField()->getFolderName();
252+
$upload = Upload::create();
215253

216-
/** @var AssetContainer|File $file */
217-
$file = $upload->getFile();
218-
$file->ShowInSearch = 0;
219-
$file->UserFormUpload = UserFormFileExtension::USER_FORM_UPLOAD_TRUE;
220-
$file->write();
254+
try {
255+
$result = $upload->loadIntoFile($file, null, $foldername);
221256

222-
$submittedField->UploadedFileID = $file->ID;
223-
} catch (ValidationException $e) {
257+
/** @var AssetContainer|File $fileObj */
258+
$fileObj = $upload->getFile();
224259

225-
}
260+
if ($result && $fileObj) {
261+
$fileObj->ShowInSearch = 0;
262+
$fileObj->UserFormUpload = UserFormFileExtension::USER_FORM_UPLOAD_TRUE;
263+
$fileObj->write();
264+
265+
$submittedField->UploadedFileID = $fileObj->ID;
266+
} else {
267+
throw new Exception('Could not upload files: %s', implode($upload->getErrors()));
226268
}
269+
} catch (ValidationException $e) {
270+
Injector::inst()->get(LoggerInterface::class)->error($e);
227271
}
228272
}
229273

0 commit comments

Comments
 (0)