Skip to content

Commit 3b0e80a

Browse files
committedNov 23, 2022
Fix for validating uploads with a Rules\File::class instance
1 parent 9b2362c commit 3b0e80a

File tree

7 files changed

+83
-3
lines changed

7 files changed

+83
-3
lines changed
 

‎UPGRADING.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
# To v0.4
1+
# Upgrade Guide
2+
3+
## From v0.x to v1.0
4+
5+
If you've published the vendor Blade templates in v0.x, they all ended up in `/resources/views/vendor/splade`. Starting with v1.0, the templates are being published in that folder but are split into a `components` and `functional` subfolder. The `functional` folder won't be published as there's nothing to customize. Check out the [default repository](https://github.com/protonemedia/laravel-splade/tree/main/resources/views) to see the new grouping of the templates.
6+
7+
## To v0.4
28

39
* Default data passed to `<x-form>` now requires the `unguarded` attribute when binding an Eloquent Model or Fluent instance. This is not necessary when using the new Form Components.
410
* The new Form Components come with Choices.js and Flatpickr integrations. Splade now comes with a default stylesheet to give the libraries some basic styling. This is completely optional, but you probably want to import this default stylesheet into your main JavaScript file:
@@ -11,7 +17,7 @@ import { createApp } from "vue";
1117
...
1218
```
1319

14-
# To v0.2
20+
## To v0.2
1521

1622
* There's a new Blade `@spladeHead` directive to support title and meta tags. You may add this directive to your root template.
1723
* Splade now supports SSR. This is optional, so you may follow the [manual installation guide](https://splade.dev/docs/ssr) to set up SSR.

‎app/app/Http/Controllers/FilepondController.php

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Controllers;
44

55
use App\Http\Requests\AvatarUpload;
6+
use App\Http\Requests\AvatarUploadFileRule;
67
use Illuminate\Http\Request;
78
use Illuminate\Routing\Controller;
89
use ProtoneMedia\Splade\FileUploads\HandleSpladeFileUploads;
@@ -67,4 +68,11 @@ public function storeWithFormRequest(AvatarUpload $request)
6768

6869
return redirect()->route('navigation.one');
6970
}
71+
72+
public function storeWithFormRequestRuleObject(AvatarUploadFileRule $request)
73+
{
74+
$request->file('avatar')->storeAs('avatars', 'avatar.jpg');
75+
76+
return redirect()->route('navigation.one');
77+
}
7078
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
use Illuminate\Validation\Rules\File;
7+
use ProtoneMedia\Splade\FileUploads\HasSpladeFileUploads;
8+
9+
class AvatarUploadFileRule extends FormRequest implements HasSpladeFileUploads
10+
{
11+
/**
12+
* Determine if the user is authorized to make this request.
13+
*
14+
* @return bool
15+
*/
16+
public function authorize()
17+
{
18+
return true;
19+
}
20+
21+
/**
22+
* Get the validation rules that apply to the request.
23+
*
24+
* @return array<string, mixed>
25+
*/
26+
public function rules()
27+
{
28+
return [
29+
'avatar' => ['required', File::image()],
30+
];
31+
}
32+
}

‎app/resources/views/form/components/filepond.blade.php

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
<x-splade-submit />
3535
</x-splade-form>
3636

37+
<x-splade-form dusk="form-request-rule-object" :action="route('form.components.filepond.storeWithFormRequestRuleObject')" class="space-y-4">
38+
<x-splade-file filepond name="avatar" server />
39+
<x-splade-submit />
40+
</x-splade-form>
41+
3742
<x-splade-form dusk="js-config">
3843
<x-splade-file filepond="{ credits: null }" name="file" />
3944
</x-splade-form>

‎app/routes/web.php

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
->name('form.components.filepond.storeWithRouteMiddleware');
153153

154154
Route::post('form/components/storeWithFormRequest', [FilepondController::class, 'storeWithFormRequest'])->name('form.components.filepond.storeWithFormRequest');
155+
Route::post('form/components/storeWithFormRequestRuleObject', [FilepondController::class, 'storeWithFormRequestRuleObject'])->name('form.components.filepond.storeWithFormRequestRuleObject');
155156

156157
Route::view('form/components/selectPlaceholder', 'form.components.selectPlaceholder')->name('form.components.selectPlaceholder');
157158

‎app/tests/Browser/Form/FilepondTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,23 @@ public function it_can_upload_a_temporary_file_using_a_form_request()
134134
});
135135
}
136136

137+
/** @test */
138+
public function it_can_upload_a_temporary_file_using_a_form_request_that_has_a_validation_rule_object()
139+
{
140+
$this->browse(function (Browser $browser) {
141+
$browser->visit('form/components/filepond')
142+
->waitForText('FormFilePond')
143+
->within('@form-request-rule-object', function (Browser $browser) {
144+
$browser->attach('avatar', __DIR__ . '/../small.jpeg')
145+
->waitForText('Upload complete')
146+
->press('Submit');
147+
})
148+
->waitForRoute('navigation.one');
149+
150+
$this->assertFileExists(storage_path('app/avatars/avatar.jpg'));
151+
});
152+
}
153+
137154
/** @test */
138155
public function it_can_use_a_custom_js_config()
139156
{

‎src/FileUploads/HandleSpladeFileUploads.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Support\Collection;
1111
use Illuminate\Support\Facades\Validator;
1212
use Illuminate\Support\Str;
13+
use Illuminate\Validation\Rules\File;
1314

1415
class HandleSpladeFileUploads extends TransformsRequest
1516
{
@@ -78,7 +79,17 @@ public static function forFormRequest(FormRequest $formRequest): FormRequest
7879
$rules = Validator::make([], $formRequest->rules())->getRules();
7980

8081
$keys = Collection::make($rules)->filter(function ($rules) {
81-
return in_array('file', $rules);
82+
if (in_array('file', $rules)) {
83+
return true;
84+
}
85+
86+
foreach ($rules as $rule) {
87+
if ($rule instanceof File) {
88+
return true;
89+
}
90+
}
91+
92+
return false;
8293
})->keys()->values();
8394

8495
return static::forRequest($formRequest, $keys->isEmpty() ? null : $keys->all());

0 commit comments

Comments
 (0)
Please sign in to comment.