Yii Framework Forum

Validation of multiple documents filesize


(satya1328) #1

Hi,
I am new to yii framework. Working with the multiple documents uploading in my application. I want to validate the file size for 2mb only. I have written the following code in my wizard.js file as below.

$(document).ready(function() {

$.validator.addMethod('findfilesize', function (value, element, param) {
        for(var i = 0 ; i < element.files.length ; i++){
        return this.optional(element) || (element.files[i].size <= param)
    }
}, 'File size must be less than {0}'); 

$(".nexttab").click(function() {
        var form = $(".project_create");
        form.validate({
            errorElement: 'span',
            errorClass: 'help-block',
            highlight: function(element, errorClass, validClass) {
                $(element).closest('.form-group').addClass("has-error");
            },
            unhighlight: function(element, errorClass, validClass) {
                $(element).closest('.form-group').removeClass("has-error");
            },
            rules: {
                        "Projects[document_name][]": {
                    extension: "pdf|doc|docx|txt",
                    findfilesize: 2097152
                }, 
          },
            messages: {
                     "Projects[document_name][]": {
                    extension: "Only file type pdf/doc/docx/txt are allowed",
                    findfilesize: "Document size should be lessthan 2MB"
                }, 
    }
        });

By using the above code, I am able to validate the filesize. But my form is submitting when any document after the first file is exceeding the limit.

Please help me to fix the issue. It was a high priority for me. Thanks in advance.

Regards,
Satya.


(Alirz23) #2

if you want your form to prevent the default action call preventDefault on event

$(".nexttab").click(function(e) {
    e.preventDefault();