Not a bad solutions, but there are two problems.

1. errorClass not being set when invalidHandler is called is certainly
a design flaw, unless a reason can be pointed out otherwise, and
2. neither errorList nor errorMap are documented, and per
http://docs.jquery.com/Plugins/Validation, "The validator object has
more methods, but only those documented here are intended for usage."
This means usage of errorList or errorMap will not necessarily be
supported in future versions, nor are they guaranteed to contain the
same values even if they do remain supported.

At any rate, the solution was to use the "errorPlacement" method. It
passes both the error message and the invalid element, and allows you
to determine what to do with the error on a per element basis:


errorPlacement: function(error, element) {
        if (element.attr('name').indexOf('category_selector') !== -1)
                $('div.error').html("You Missed A Category Selection. It has 
been
highlighted below");
        else error.insertAfter(element);
}

On Apr 2, 8:44 pm, Mac <amcint...@bigmate.com.au> wrote:
> Hi
>
> The validator plugin contains errorList and errorMap properties. The
> erorrList contains the invalid elements and the respective messages
> and the errorMap contains the invalid element id and it's associated
> error message. Perhaps checking these will assist with determining the
> invalid element.
>
> eg. if (validator.errorMap.selectElementId == 'undefined') { execute
> code}
>
> Using undefined since you have no errormessage to test against.
>
> Hope that helps...
>
> On Apr 3, 7:04 am, CrabMan <atcrabt...@gmail.com> wrote:
>
> > This makes it very difficult to determine which element is causing the
> > error and execute code in response.
>
> > For instance, the following does not work, because there is no way to
> > determine which input caused the error because the errorClass has not
> > been set yet when invalidHandler is called:
>
> > $('#step_1_form').validate({
> >         rules: {
> >                 category_selector: "required",
> >                 zip_code: "required"
> >         },
> >         messages: {
> >                 category_selector: ""
> >         },
> >         invalidHandler: function() {
> >                 if 
> > ($('select[name^="category_selector"][class*="error"]').length >
> > 0)
> >                 {
> >                         // EXECUTE SOME CODE SPECIFIC TO THIS ERROR
> >                 }
> >         }
>
> > }
>
> > A work around is to use setTimeout, but is obviously an undesirable
> > hack:
>
> > $('#step_1_form').validate({
> >         rules: {
> >                 category_selector: "required",
> >                 zip_code: "required"
> >         },
> >         messages: {
> >                 category_selector: ""
> >         },
> >         invalidHandler: function() {
> >                 var displayErrors = function() {
> >                         if 
> > ($('select[name^="category_selector"][class*="error"]').length >
> > 0)
> >                         {
> >                                 // EXECUTE SOME CODE SPECIFIC TO THIS ERROR
> >                         }
> >                 };
> >                 setTimeout(displayErrors,10);
> >         }
>
> > }
>
> > Am I correct? or is there a better way to determine the element
> > causing the error, and regardless, shouldn't errorClass be set before
> > invalidHandler is called anyways?

Reply via email to