Skip to content

Commit 220e480

Browse files
committed
fixed uploads
1 parent 74e2e43 commit 220e480

File tree

6 files changed

+56
-49
lines changed

6 files changed

+56
-49
lines changed

dist/js/field.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<script>
2+
import FileField from '@/components/Form/FileField'
3+
import { Errors } from 'laravel-nova'
4+
5+
export default {
6+
mixins: [FileField],
7+
8+
methods: {
9+
/**
10+
* Remove the linked file from storage
11+
*/
12+
async removeFile() {
13+
this.uploadErrors = new Errors()
14+
15+
const {
16+
resourceName,
17+
resourceId,
18+
relatedResourceName,
19+
relatedResourceId,
20+
viaRelationship
21+
} = this
22+
const attribute = this.field.original_attribute
23+
24+
const uri = this.viaRelationship
25+
? `/nova-api/${resourceName}/${resourceId}/${relatedResourceName}/${relatedResourceId}/field/${attribute}?viaRelationship=${viaRelationship}`
26+
: `/nova-api/${resourceName}/${resourceId}/field/${attribute}`
27+
28+
try {
29+
await Nova.request().delete(uri)
30+
this.closeRemoveModal()
31+
this.deleted = true
32+
this.$emit('file-deleted')
33+
} catch (error) {
34+
this.closeRemoveModal()
35+
36+
if (error.response.status == 422) {
37+
this.uploadErrors = new Errors(error.response.data.errors)
38+
}
39+
}
40+
}
41+
}
42+
}
43+
</script>

resources/js/components/FormField.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
:index="index"
1717
:field="field"
1818
:child="child"
19-
:errors="errors" />
19+
:errors="errors"
20+
@file-deleted="$emit('file-deleted')" />
2021
<!-- ACTUAL FIELDS -->
2122
</template>
2223

resources/js/components/NestedFormField.vue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@
3535
:field="subfield"
3636
:key="subfield.attribute"
3737
:errors="errors"
38+
:resource-id="child[field.ID]"
3839
:resource-name="field.resourceName"
3940
:via-resource="field.viaResource"
4041
:via-resource-id="field.viaResourceId"
4142
:via-relationship="field.viaRelationship"
43+
:related-resource-name="field.relatedResourceName"
44+
:related-resource-id="field.relatedResourceId"
45+
@file-deleted="$emit('file-deleted')"
4246
:is="`form-${getComponent(subfield)}`" />
4347
<!-- ACTUAL FIELDS -->
4448

@@ -56,11 +60,12 @@
5660

5761
<script>
5862
import FormNestedBelongsToField from './CustomNestedFields/BelongsToField'
63+
import FormNestedFileField from './CustomNestedFields/FileField'
5964
import DeleteModal from './Modals/Delete'
6065
6166
export default {
6267
63-
components: { FormNestedBelongsToField, DeleteModal },
68+
components: { FormNestedBelongsToField, FormNestedFileField, DeleteModal },
6469
6570
6671
props: {
@@ -153,7 +158,7 @@ export default {
153158
*/
154159
getComponent(field) {
155160
156-
if (field.component === 'belongs-to-field') {
161+
if (['belongs-to-field', 'file-field'].includes(field.component)) {
157162
return 'nested-' + field.component
158163
}
159164

src/NestedForm.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ public function jsonSerialize()
230230
'component' => $this->component,
231231
'prefixComponent' => true,
232232
'resourceName' => $this->resourceName,
233-
'viaRelationship' => $this->viaRelationship,
234233
'listable' => true,
235234
'singularLabel' => Str::singular($this->name),
236235
'pluralLabel' => Str::plural($this->name),

src/Traits/FillsSubAttributes.php

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
use Laravel\Nova\Http\Requests\NovaRequest;
1717
use Laravel\Nova\Http\Requests\UpdateResourceRequest;
1818
use Laravel\Nova\Nova;
19+
use Symfony\Component\HttpFoundation\FileBag;
1920
use Symfony\Component\HttpFoundation\ParameterBag;
2021
use Yassi\NestedForm\Exceptions\NestedValidationException;
2122
use Yassi\NestedForm\NestedForm;
22-
use Symfony\Component\HttpFoundation\File\UploadedFile;
2323

2424
trait FillsSubAttributes
2525
{
@@ -92,6 +92,8 @@ protected function newRequest(array $data, Model $model, string $attribute, int
9292
$request = CreateResourceRequest::createFrom($this->request);
9393
}
9494

95+
$request->files = new FileBag($this->request->file($attribute)[$index ?? 0] ?? []);
96+
9597
$request->query = new ParameterBag($this->query($data, $attribute, $index));
9698

9799
$request->replace($this->data($data, $model));
@@ -191,8 +193,6 @@ protected function attribute(string $attribute, int $index = null)
191193
return $attribute . ($this->isManyRelationship() ? '[' . $index . ']' : '');
192194
}
193195

194-
195-
196196
/**
197197
* This method loops through the request data
198198
* and runs the necessary controller with the right
@@ -214,10 +214,6 @@ protected function runNestedOperations(NovaRequest $request, string $attribute,
214214
$index = null;
215215
}
216216

217-
if ($this->containsFile($attribute, $index)) {
218-
$request->files->add($this->getFiles($attribute, $index));
219-
}
220-
221217
$this->runNestedOperation($value, $model, $attribute, $index, $request);
222218

223219
if ($value instanceof Response) {
@@ -237,43 +233,6 @@ protected function runNestedOperations(NovaRequest $request, string $attribute,
237233
return $this;
238234
}
239235

240-
/**
241-
* Check if the nested item contains a file.
242-
*
243-
* @param string $attribute
244-
* @param int $index
245-
* @return bool
246-
*/
247-
protected function containsFile(string $attribute, int $index = null)
248-
{
249-
if (is_null($index)) {
250-
return isset($_FILES[$attribute]);
251-
}
252-
253-
return isset($_FILES[$attribute]) && isset($_FILES[$attribute]['name'][$index]);
254-
}
255-
256-
/**
257-
* Change the file attribute to handle the download properly.
258-
*
259-
* @param string $attribute
260-
* @param int $index
261-
* @return array
262-
*/
263-
protected function getFiles(string $attribute, int $index = 0)
264-
{
265-
$attributes = array_keys($_FILES[$attribute]['name'][$index]);
266-
$files = [];
267-
268-
foreach ($attributes as $newAttribute) {
269-
$newName = $_FILES[$attribute]['tmp_name'][$index][$newAttribute];
270-
$newPath = $_FILES[$attribute]['tmp_name'][$index][$newAttribute];
271-
$files[] = [$newAttribute => new UploadedFile($newPath, $newName)];
272-
}
273-
274-
return $files;
275-
}
276-
277236
/**
278237
* This method runs the necessary controller with the right
279238
* request.

0 commit comments

Comments
 (0)