3

I have my form as below

<div id="new_image" title="Add Image">
    <form id="new_image_form" action="/reg_new_img" method="POST" enctype="multipart/form-data">
        <table width="0" border="0" cellspacing="0" cellpadding="6" class="table100">
<span class="astrix"></span>

            <tr>
                <td class="td30">Description of the Image:</td>
                <td>
                    <input name="desc" type="text" value="" />
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input id="atag" type="hidden" name="atag" value="">
                    </textarea>
                </td>
            </tr>
            <tr>
                <tr>
                    <td>Files / Image:
                        <p class="text5">X-ray / Site / Document / Agreement etc.</p>
                    </td>
                    <td>
                        <input name="image" type="file">
                    </td>
                </tr>
        </table>
    </form>
</div>

the script is as below.

$(document).ready(function () {

    $("#new_image").dialog({
        height: $("#new_image").height() + 150,
        width: $("#new_image").width() - 450,
        modal: true,
        autoOpen: false,
        show: {
            effect: "fade",
            duration: 200
        },
        hide: {
            effect: "fade",
            duration: 200
        },

        buttons: {
            "Submit": function () {
                $("#new_image_form").submit();
            },
                "Close": function () {
                $('#new_image').dialog("close");
            }
        }
    });

    $('#new_image_form').validate({
        rules: {
            desc: {
                required: true,
                minlength: 10
            },
            image: {
                required: true,
                accept: "image/*"
            }
        },
        messages: {
            image: {
                required: "only Image files accepted",
                accept: "Please upload only image",
            },
        },
        submitHandler: function (form) {
            $('#new_image_form').ajaxSubmit({
                success: function (data) {
                    console.log(data);
                    $('#new_image').dialog("close");
                    $(':input', '#new_image_form')
                        .not(':button, :submit, :reset, :hidden')
                        .val('')
                        .removeAttr('checked')
                        .removeAttr('selected');
                }
            });
            parse_patients();
        }
    });
    $('#new_image').dialog("open");
});

I have this code on js fiddle

http://jsfiddle.net/rakeshpatil1983/ctL7q/

I'm getting an error when i click submit when I actually put image file on the chrome browser console

Uncaught TypeError: Cannot call method 'call' of undefined

I'm not able to fix this. This code seems to work in some other page.

rakesh
  • 975
  • 2
  • 11
  • 15
  • You'll need to also include the `additional-methods.js` file if you want to use the `accept` rule. – Sparky May 31 '13 at 14:43

2 Answers2

11

In your rules for #new_image_form, "accept" is not a default jQuery Validator default rule. You must either use one of these default rules: Jquery Validate - List of Classes or define your own custom rule like such: jQuery Validate Plugin - How to create a simple custom rule?

EDIT: from comments below: there is a rule called accept that is part of the jQuery Validate plugin package. You simply have to include the additional-methods file in order to use it.

Community
  • 1
  • 1
755
  • 2,991
  • 3
  • 20
  • 34
  • 1
    And the reason you are getting "cannot call 'call' on undefined" is that jQuery is looking for a method called "accept", not finding one (returning undefined), and then trying to call the method (which is undefined). – 755 May 31 '13 at 00:13
  • 1
    the accept method is in additional-methods.js https://github.com/jzaefferer/jquery-validation/blob/master/additional-methods.js – politus May 31 '13 at 14:40
  • This answer is off the mark... there **is** a rule called `accept` that is part of the jQuery Validate plugin package. You simply have to include the `additional-methods` file in order to use it. – Sparky May 31 '13 at 14:57
-1
//Add New Validator Rule
jQuery.validator.addMethod("accept", function(value, element, param)
{
    return value.match(new RegExp("^" + param + "$"));
});

I had similar problem, which 'accept' rule was forgot to declared in the beginning.

then 'Uncaught TypeError: Cannot call method 'call' of undefined' disappeared.

Qin Wang
  • 422
  • 4
  • 12