FileUpload

Livewire native file upload

Requirements

  • This field requires you to configure Livewire file uploads according to the main documentation. Setup storage disk and default file validation. This component handles the rest.

  • Read livewire docs about scheduling cleanup of the temporary storage folder

  • Read livewire docs about saving the files

Add required traits to your component

use Tanthammar\TallForms\Traits\UploadsFiles;
use Livewire\WithFileUploads;

class someForm extends Component
{
    use WithFileUploads, UploadsFiles;
}

Add required properties

Livewire requires you to add the property for file-upload fields, to the form component

//properties used in this example
//Do NOT add type declaration = Livewire error...
public $image;
public $files;

Validation messages

Set default validation message in the config file

Livewire default validation rules are in its config file. This is fine until you want to use custom validation. The problem is that the file gets uploaded before your custom validation executes, and the file remains on the server. The FileUpload field has a workaround that deletes the temporary file if your custom validation fails. The user is presented with a general error that you define in the language files. This component tries to minimize unwanted files in the temp folder, by forcing the user to delete uploaded files before they can upload new ones.

//languange files
'file-upload' => [
    'upload-file-error' => '...',
]

Cleanup old uploads

  • Not using s3: Livewire will handle the file cleanup automatically.

  • Using s3: You must schedule the cleanup

php artisan livewire:configure-s3-upload-cleanup

Saving the files

You must save the files yourself !!!

  • The data is accessed via the fields name property, like $this->fieldname.

  • (Read Livewire docs about various ways to store uploaded files)

  • (Read more about Lifecycle Hooks to understand saveFoo())

The FileUpload data is never available in the form_data property, you access it with $this->foo because Livewire requires you to define the property on the component.

Last updated