Skip to content

Commit

Permalink
Allow Server Side validation during upload (#25)
Browse files Browse the repository at this point in the history
* Allow Server Side validation during upload

* Multiple file upload needs a reset

* Update README.md

* Update README.md

---------

Co-authored-by: Sébastien Morel <[email protected]>
  • Loading branch information
dragonfly4 and Sébastien Morel authored Oct 18, 2024
1 parent 8e69e56 commit b059c4a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,35 @@ App::setLocale('id'); // change to Indonesian for example
```
The last method can be used to change the locale on the fly. Like when a user changes the language (You need to implement this yourself).

## Server Side Validation on upload

Optionally, you can validate the uploaded file immediately. This is useful to inform the user of an error and process file uploads without requiring the user to click a button.

```php
use Livewire\Component;
use Spatie\LivewireFilepond\WithFilePond;

class MyLivewireComponent extends Component
{
use WithFilePond;

public $file;

public function rules(): array
{
return [
'file' => 'required|mimetypes:image/jpg,image/jpeg,image/png|max:3000',
];
}
public function validateUploadedFile()
{
$this->validate();

return true;
}
}
```

## Publishing assets

Livewire Filepond automatically loads the scripts through an endpoint. If you want to serve the assets directly, you can publish them:
Expand Down
15 changes: 12 additions & 3 deletions resources/views/upload.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,18 @@ class="{{ $attributes->get('class') }}"
server: {
process: async (fieldName, file, metadata, load, error, progress) => {
$dispatch('filepond-upload-started', '{{ $wireModelAttribute }}');
await @this.upload('{{ $wireModelAttribute }}', file, (response) => {
load(response);
$dispatch('filepond-upload-finished', {'{{ $wireModelAttribute }}': response });
await @this.upload('{{ $wireModelAttribute }}', file, async (response) => {
let validationResult = await @this.call('validateUploadedFile', response);
// Check server validation result
if (validationResult === true) {
// File is valid, dispatch the upload-finished event
load(response);
$dispatch('filepond-upload-finished', { '{{ $wireModelAttribute }}': response });
} else {
// Throw error after validating server side
error('Filepond Api Ignores This Message');
$dispatch('filepond-upload-reset', '{{ $wireModelAttribute }}');
}
}, error, progress);
},
revert: async (filename, load) => {
Expand Down
8 changes: 8 additions & 0 deletions src/WithFilePond.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ public function revert($property, $filename): void
app(LivewireManager::class)->updateProperty($this, $property, $newFiles);
}

/**
* Default returns true, override in your component
*/
public function validateUploadedFile(): bool
{
return true;
}

public function resetFilePond(string $property): void
{
$this->reset($property);
Expand Down

0 comments on commit b059c4a

Please sign in to comment.