question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Does this plugin supports validation for image file size?

See original GitHub issue

How can you use vuelidate with image uploading validation? Currently I’m uploading the image and pass it to the API using base64. Is there I way when I upload image it will validate that the image file size cannot be higher than 5mb?

screenshot 2019-01-29 00 33 13

currently I only handle the required field.

import { required } from 'vuelidate/lib/validators';
validations: {
  profileImage: { required }
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
guillem-catalacommented, Apr 5, 2020

What about something like this

import { helpers } from 'vuelidate/lib/validators'

export const isValidFileSize = (options = {}) => {
  return helpers.withParams(options, value => {
    if (!value) {
      return true
    }
    const fileSizeinKb = value.size / 1024
    const size = Math.round(fileSizeinKb * 100) / 100 // convert up to 2 decimal place
    return size <= options.maxFileSizeInB / 1024
  })
}

1reaction
mpm-myokocommented, Jul 17, 2020

With reference to the above code, I adapted to my requirement. The validation:

const isTrueImage = (value) => {
	if (!value) {
    	    return true;
  	}
  	let file = value;
  	return file.type.startsWith('image');
}

The template:

<div class="my-3 p-3 border" id="logo">
	<div class="form-group">
		<label>Logo</label>
		<div :class="['custom-file',$v.form.logo.$error?'is-invalid':'']">
			<input type="file" class="custom-file-input" @change="onLogoChange($event)">
			<label class="custom-file-label">Choose File</label>
		</div>
		<div class="invalid-feedback">
                    <div class="error" v-if="!$v.form.logo.isTrueImage">Invalid image file.</div>
               </div>
	</div>
	<img :src="form.logo_url" class="rounded-circle img-thumbnail logo"  />
</div>

The component:

export default {
	data() {
		return {			
			form: {				
				logo: '',
				logo_url: '',
			},
		}
	},
	methods: {
		onLogoChange(e) {			
			const files =  e.target.files || e.dataTransfer.files;
			const file = files[0];
                        this.form.logo = file;  // --Important: you have to set the value here
                        this.$v.form.logo.$touch(); // --And validate with the plugin
			if (this.$v.form.logo.$error) return;			
			this.form.logo_url = URL.createObjectURL(file);
		}
	},	
	validations: {
		form: {
			logo: { isTrueImage },
		}
	}
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Image validate size - FilePond - PQINA
Property Default value Description allowImageValidateSize true Enable or disable image size validation. | imageValidateSizeMinWidth 1 The minimum image width. | imageValidateSizeMaxWidth 65535 The maximum image width....
Read more >
How to validate review image size on single product page?
It turns out the plugin does server-side validation of image size/quantity and no client-side validation. I have used jQuery to implement validation ...
Read more >
Validate image filetype/filesize, crop, resize & then upload
I'm looking for a client side solution to validate whether the image being uploaded is of accepted file type, ...
Read more >
filepond-plugin-image-validate-size - npm
Image Size Validation Plugin for FilePond. Latest version: 1.2.7, last published: 10 months ago.
Read more >
file validator - FormValidation
The following form allows to upload JPEG, PNG image which is smaller than 2 MB in size. Use the promise validator if you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found