I wanted to check which validation way is correct.
I tried 3 ways to upload mediafile which more than 30M .
and the validation limit of uploading file is up to 1 MB.
So I used 'max:1024'.
- validating when file saves.
this validation works, but not shows error message on view.
class FilesUpload extends Component {
public $files = [];
function saveUpload(){
$this->validate([
'files.*' => 'file|mimes:mp4,mkv,srt,txt|max:1024', // 1MB Max
]);
// do Save..
}
}
2 . validating when file updated.
this validation DOES NOT work, it passed through validation.
class FilesUpload extends Component {
public $files = [];
public function updatedFiles()
{
$this->validate([
'files.*' => 'file|mimes:mp4,mkv,srt,txt|max:1024', // 1MB Max
]);
}
function saveUpload(){
// skip validation here.
// do Save..
}
}
3 . validating with laravel style(?).
Also this validation DOES NOT work, it passed through validation.
class FilesUpload extends Component {
public $files = [];
protected $rules = [
'files.*' => 'file|mimes:mp4,mkv,srt,txt|max:1024', // 1MB Max
];
function saveUpload(){
// skip validation here.
// do Save..
}
}
I used alpine.js for controlling upload. Following is the view.
// files-upload.blade.php
<div x-data="fileUpload()">
<input type="file" @change="handleFileSelect" />
<div>
@error('files') <span class="error">{{ $message }}</span> @enderror
</div>
<button wire:click="saveUpload()" class="btn btn-primary "> Save </button>
<script>
function fileUpload() {
return {
isDropping: false,
isUploading: false,
progress: 0,
handleFileSelect(event) {
if (event.target.files.length) {
this.uploadFiles(event.target.files)
}
},
uploadFiles(files) {
const $this = this;
this.isUploading = true;
@this.uploadMultiple('files', files, function(success) {
$this.isUploading = false;
$this.progress = 0;
}, function(error) {
console.log('error', error);
}, function(event) {
$this.progress = event.detail.progress
})
},
}
}
</script>
</div>
How can I make livewire validation works correctly ?