Adam Rackis
Adam Rackis

Reputation: 83356

jQuery validate - specify only errorPlacement

I'm having difficulty getting error placement to work. I have a series of forms on my page, each using the built-in validation classes—required, number, etc—but for some reason the error placement isn't working. At all.

The rest of the form validation works great, but I'm not getting my alert to show.

$(".bundle-forms-form-container form").validate({
    errorPlacement: function(error, element) {
        alert("X");
        var customTarget = $(element).closest("form").find(".errorHere");
        if (true || customTarget.length)
            customTarget.html(error);
        //else
            error.insertAfter(element);
    }
});

Upvotes: 1

Views: 465

Answers (1)

Adam Rackis
Adam Rackis

Reputation: 83356

It looks like the problem is that jQuery validate loses its mind if you call validate on a result set with multiple forms. The solution is simple

$(".bundle-forms-form-container form").each(function(i, el){
    $(el).validate({
        errorPlacement: function(error, element) {
            var customTarget = $(element).closest("form").find(".errorHere");
            if (customTarget.length)
                customTarget.empty().append(error);
            else
                error.insertAfter(element);
        }
    });
});

Upvotes: 2

Related Questions