Skip to content

Commit d412a31

Browse files
authored
fix: File nested state path validation (#19201)
1 parent 1759841 commit d412a31

File tree

2 files changed

+153
-1
lines changed

2 files changed

+153
-1
lines changed

packages/forms/src/Components/BaseFileUpload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ public function getValidationRules(): array
641641
$rules[] = function (string $attribute, array $value, Closure $fail) use ($fileRules): void {
642642
$files = array_filter($value, fn (TemporaryUploadedFile | string $file): bool => $file instanceof TemporaryUploadedFile);
643643

644-
$name = $this->getName();
644+
$name = Str::afterLast($this->getName(), '.');
645645

646646
$validationMessages = $this->getValidationMessages();
647647

tests/src/Forms/Components/FileUploadTest.php

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,86 @@
193193
$stringRules = array_filter($rules, fn ($rule) => is_string($rule));
194194
expect($stringRules)->not->toContain('mimetypes:image/png');
195195
});
196+
197+
it('can use `maxSize()` and fails validation when file exceeds limit', function (): void {
198+
livewire(TestComponentWithMaxSizeFileUpload::class)
199+
->fillForm([
200+
'document' => UploadedFile::fake()->create('document.pdf', 200),
201+
])
202+
->call('save')
203+
->assertHasFormErrors(['document']);
204+
});
205+
206+
it('can use `maxSize()` and passes validation when file is within limit', function (): void {
207+
livewire(TestComponentWithMaxSizeFileUpload::class)
208+
->fillForm([
209+
'document' => UploadedFile::fake()->create('document.pdf', 50),
210+
])
211+
->call('save')
212+
->assertHasNoFormErrors(['document']);
213+
});
214+
215+
it('can use `minSize()` and fails validation when file is below limit', function (): void {
216+
livewire(TestComponentWithMinSizeFileUpload::class)
217+
->fillForm([
218+
'document' => UploadedFile::fake()->create('document.pdf', 50),
219+
])
220+
->call('save')
221+
->assertHasFormErrors(['document']);
222+
});
223+
224+
it('can use `minSize()` and passes validation when file meets limit', function (): void {
225+
livewire(TestComponentWithMinSizeFileUpload::class)
226+
->fillForm([
227+
'document' => UploadedFile::fake()->create('document.pdf', 150),
228+
])
229+
->call('save')
230+
->assertHasNoFormErrors(['document']);
231+
});
232+
233+
it('can use `maxSize()` with nested state path and fails validation when file exceeds limit', function (): void {
234+
livewire(TestComponentWithNestedMaxSizeFileUpload::class)
235+
->fillForm([
236+
'files' => [
237+
'test' => UploadedFile::fake()->create('document.pdf', 200),
238+
],
239+
])
240+
->call('save')
241+
->assertHasFormErrors(['files.test']);
242+
});
243+
244+
it('can use `maxSize()` with nested state path and passes validation when file is within limit', function (): void {
245+
livewire(TestComponentWithNestedMaxSizeFileUpload::class)
246+
->fillForm([
247+
'files' => [
248+
'test' => UploadedFile::fake()->create('document.pdf', 50),
249+
],
250+
])
251+
->call('save')
252+
->assertHasNoFormErrors(['files.test']);
253+
});
254+
255+
it('can use `minSize()` with nested state path and fails validation when file is below limit', function (): void {
256+
livewire(TestComponentWithNestedMinSizeFileUpload::class)
257+
->fillForm([
258+
'files' => [
259+
'test' => UploadedFile::fake()->create('document.pdf', 50),
260+
],
261+
])
262+
->call('save')
263+
->assertHasFormErrors(['files.test']);
264+
});
265+
266+
it('can use `minSize()` with nested state path and passes validation when file meets limit', function (): void {
267+
livewire(TestComponentWithNestedMinSizeFileUpload::class)
268+
->fillForm([
269+
'files' => [
270+
'test' => UploadedFile::fake()->create('document.pdf', 150),
271+
],
272+
])
273+
->call('save')
274+
->assertHasNoFormErrors(['files.test']);
275+
});
196276
});
197277

198278
class TestComponentWithFileUpload extends Livewire
@@ -207,3 +287,75 @@ public function form(Schema $form): Schema
207287
->statePath('data');
208288
}
209289
}
290+
291+
class TestComponentWithMaxSizeFileUpload extends Livewire
292+
{
293+
public function form(Schema $form): Schema
294+
{
295+
return $form
296+
->components([
297+
FileUpload::make('document')
298+
->maxSize(100),
299+
])
300+
->statePath('data');
301+
}
302+
303+
public function save(): void
304+
{
305+
$this->form->getState();
306+
}
307+
}
308+
309+
class TestComponentWithMinSizeFileUpload extends Livewire
310+
{
311+
public function form(Schema $form): Schema
312+
{
313+
return $form
314+
->components([
315+
FileUpload::make('document')
316+
->minSize(100),
317+
])
318+
->statePath('data');
319+
}
320+
321+
public function save(): void
322+
{
323+
$this->form->getState();
324+
}
325+
}
326+
327+
class TestComponentWithNestedMaxSizeFileUpload extends Livewire
328+
{
329+
public function form(Schema $form): Schema
330+
{
331+
return $form
332+
->components([
333+
FileUpload::make('files.test')
334+
->maxSize(100),
335+
])
336+
->statePath('data');
337+
}
338+
339+
public function save(): void
340+
{
341+
$this->form->getState();
342+
}
343+
}
344+
345+
class TestComponentWithNestedMinSizeFileUpload extends Livewire
346+
{
347+
public function form(Schema $form): Schema
348+
{
349+
return $form
350+
->components([
351+
FileUpload::make('files.test')
352+
->minSize(100),
353+
])
354+
->statePath('data');
355+
}
356+
357+
public function save(): void
358+
{
359+
$this->form->getState();
360+
}
361+
}

0 commit comments

Comments
 (0)