Search code examples
jquerytwitter-bootstrapformsvalidationbootstrapvalidator

bootstrap validate input type file not working


I'm having trouble getting a bootstrap validation for an input type of 'file' to work. The form is validating even if I do not select a file to upload. It is supposed to require at least one file, of a certain type and a maximum size. I can tell that the validation is coming back as validated because I get the green checkmark at the far right of the field. I'm fairly new to bootstrap but I can't figure this one out. Any help would be greatly appreciated!

<html>
    <head>
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="http://code.jquery.com/jquery-2.2.3.min.js"></script>
        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" type="text/javascript"></script><!-- Bootstrap 3.3.6 -->
        <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.2/js/bootstrapValidator.js" type="text/javascript"></script>
        <script src="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-filestyle/2.1.0/bootstrap-filestyle.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('#frm_resumeupload').bootstrapValidator({
                    message: 'This value is not valid',
                    feedbackIcons: {
                        valid: 'glyphicon glyphicon-ok',
                        invalid: 'glyphicon glyphicon-remove',
                        validating: 'glyphicon glyphicon-refresh'
                    },
                    fields: {
                        resume: {
                            validators: {
                                file: {
                                    minFiles: 1,
                                    extension: 'doc,docx,pdf,rtf,txt',
                                    type: 'application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/rtf,text/plain',
                                    maxSize: 5120 * 5120 * 5120 * 5120 * 5120,   // 5 MB
                                    message: 'The selected file is not valid, it should be (doc,docx,pdf,rtf,txt) and 5 MB at maximum.'
                                }
                            }
                        }
                    }
                });
                $("#resume_submitBtn").click(function () {
                    $('#frm_resumeupload').bootstrapValidator('validate');
                });    
            });
        </script>
    </head>
<body>
<div class="container">
    <form name="frm_resumeupload" id="frm_resumeupload" method="post" enctype="multipart/form-data">                    
        <div class="form-group">
            <input name="resume" id="resume" type="file" class="filestyle" data-text="Select New Resume" data-dragdrop="true" data-btnClass="btn-primary" data-buttonBefore="true">
        </div>
        <button type="button" id="resume_submitBtn" class="btn btn-primary" data-toggle="modal" data-target="#saveModal">Upload Resume</button>&nbsp;&nbsp;
    </form>
</div>
</body>
</html>

Solution

  • I think the issue is minFiles is not a valid parameter here. A quick fix here would be to add a required attribute to the html tag.

    e.g.:

    <input name="resume" id="resume" type="file" class="filestyle" data-text="Select New Resume" data-dragdrop="true" data-btnClass="btn-primary" data-buttonBefore="true" required>