0

I tried this custom validator to validate file extension of selected file.

I use valdr-validation-plugin with AngularJS and have written this custom validation to validate the extension of the file which is to be uploaded. Generally value parameter contains the details of the data which is to be validated, but in case of file upload its not working.

app.factory('fileValidator', function () {
return {
    name: 'fileExtension',
    validate: function (value, arguments)
    {

            if(value=== undefined) { return false; }
            else
            {
                var fileName = value.name,
                    extension = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();
                if( extension == "jpg" || extension == "jpeg" || extension == "png" || extension == "bmp" ){ return true; }
                else { return false; }
            }
        }
    };
});

In above code, I try to get the extension of file using value parameter but it showing error that value is undefined.

I call this custom validation from the below code. Register that validator to the app config and then used it to validate bannerImage.

app.config(function(valdrProvider) {
    valdrProvider.addValidator('fileValidator');
    valdrProvider.addConstraints({
        "Banner":
        {
            "bannerName":
            {
                "size":
                {
                    "min": 2,
                    "max": 512,
                    "message": "Banner name must between 2 and 512 characters"
                },
                "required" :
                {
                    "message" : "This field is required"
                }
            },
            "bannerLink":
            {
                "size":
                {
                    "min": 2,
                    "max": 1024,
                    "message": "Banner link must between 2 and 1024 characters"
                },
                "required" :
                {
                    "message" : "This field is required"
                }
            },
            "bannerImage":
            {
                "fileExtension":
                {
                    "allowedExtensions" : ['jpg','jpeg', 'png','bmp'],
                    "message" : "Allowed file types are JPG, JPEG, PNG, BMP"
                },
                "required":
                {
                    "required" : "This field is required"
                }
            }
        }
    });
});

Its not working and shows lot of errors. How can I solve this issue?

Harry
  • 87,580
  • 25
  • 202
  • 214
kishor10d
  • 543
  • 6
  • 24
  • Take a look at some workarounds to getting the value from a `` here: http://stackoverflow.com/questions/17063000/ng-model-for-input-type-file – rob Jul 15 '15 at 14:48
  • I have made a few grammar corrections in the content, removed the wrong indentation for text within the code blocks to make them visible. I have also removed the thanks note as it is not required and the framework, add-on names from the title as they are also not required due to presence of tags. – Harry Jul 15 '15 at 15:30
  • You could could do everybody who wants to help a real favor if you created an example at Plunkr e.g. fork this http://plnkr.co/edit/5J66D2EFCuhhTOYantUm and adjust accordingly. – Marcel Stör Jul 16 '15 at 17:29

0 Answers0