Skip to content

Commit d4e97ba

Browse files
committed
Fix instance id not replaced in path for single form
1 parent 86a1c6c commit d4e97ba

4 files changed

Lines changed: 31 additions & 6 deletions

File tree

docs/guide/single-form.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ class AccountSharpForm extends SharpSingleForm
5454

5555
protected function updateSingle(array $data)
5656
{
57-
$this->save(
57+
return $this->save(
5858
User::findOrFail(auth()->id()),
5959
$data
60-
);
60+
)->id;
6161
}
6262
}
6363
```

src/Http/Controllers/FormController.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,12 @@ public function update(string $parentUri, EntityKey $entityKey, ?string $instanc
135135
);
136136

137137
$formattedData = $form->formatAndValidateRequestData(request()->all(), $instanceId);
138-
$form->update($instanceId, $formattedData);
138+
$instanceId = $form->update($instanceId, $formattedData);
139+
140+
if ($instanceId === null && ! $form instanceof SharpSingleForm) {
141+
report(new SharpFormUpdateException('The update() method in '.get_class($form).' must return the newly created instance id'));
142+
}
143+
139144
$this->uploadManager->dispatchJobs($instanceId);
140145

141146
$previousUrl = request()->query('previous_page_url') ?: sharp()->context()->breadcrumb()->getPreviousSegmentUrl();

src/Http/Jobs/HandleUploadedFileJob.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Code16\Sharp\Http\Jobs;
44

5+
use Code16\Sharp\Exceptions\Form\SharpFormUpdateException;
56
use Illuminate\Bus\Queueable;
67
use Illuminate\Contracts\Queue\ShouldQueue;
78
use Illuminate\Foundation\Bus\Dispatchable;
@@ -56,7 +57,11 @@ public function handle(): void
5657

5758
private function determineFilePath(): string
5859
{
59-
if ($this->instanceId) {
60+
if (str_contains($this->filePath, '{id}')) {
61+
if ($this->instanceId === null) {
62+
throw new SharpFormUpdateException('Instance ID is required but not provided for file path template containing {id}');
63+
}
64+
6065
return str_replace('{id}', $this->instanceId, $this->filePath);
6166
}
6267

tests/Http/Jobs/HandleUploadedFileJobTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Code16\Sharp\Exceptions\Form\SharpFormUpdateException;
34
use Code16\Sharp\Http\Jobs\HandleUploadedFileJob;
45
use Illuminate\Http\UploadedFile;
56
use Illuminate\Support\Facades\Storage;
@@ -31,15 +32,29 @@
3132

3233
HandleUploadedFileJob::dispatch(
3334
uploadedFileName: 'image.jpg',
34-
filePath: 'data/things/{id}/image.jpg',
3535
disk: 'local',
36-
instanceId: 50,
36+
filePath: 'data/things/{id}/image.jpg',
3737
shouldOptimizeImage: false,
38+
instanceId: 50,
3839
);
3940

4041
Storage::disk('local')->assertExists('data/things/50/image.jpg');
4142
});
4243

44+
it('throws if instance id null and {id} in segment', function () {
45+
UploadedFile::fake()
46+
->image('image.jpg')
47+
->storeAs('/tmp', 'image.jpg', ['disk' => 'local']);
48+
49+
HandleUploadedFileJob::dispatch(
50+
uploadedFileName: 'image.jpg',
51+
disk: 'local',
52+
filePath: 'data/things/{id}/image.jpg',
53+
shouldOptimizeImage: false,
54+
);
55+
})
56+
->throws(SharpFormUpdateException::class);
57+
4358
it('optimizes uploaded images if configured', function () {
4459
$optimizer = new class()
4560
{

0 commit comments

Comments
 (0)