tags:

views:

778

answers:

3

In this function I am displaying an error image in next td of table when a field is not validated. It is displaying error images correctly but when a field is valided, it's error image is not removed from next td. I tried to use "success" option but it is not working. Can anyone tell me the exact code.

$(obj).find("form").validate({

 showErrors: function(errorMap, errorList) {
  for ( key in errorMap ) {
    $('#' + key).parent().next().html("<img id='exclamation' src='images/exclamation.gif' />");
  }
 }

});
+1  A: 

Create a function that removes all error images. Call it at the beginning of both the success and showErrors methods.

kgiannakakis
success: function() { alert('Success'); } //even this alert is not shown in my above function.
NAVEED
Even if a single validation fails, success won't be called. Are all fields validating OK?
kgiannakakis
success is called when a single field is validated also. finally I got my answer. please see below.
NAVEED
+1  A: 

You might want to look at the errorPlacement option.

see the Remember the milk demo

from there:

errorPlacement: function(error, element) {
    if ( element.is(":radio") )
        error.appendTo( element.parent().next().next() );
    else if ( element.is(":checkbox") )
        error.appendTo ( element.next() );
    else
        error.appendTo( element.parent().next() );
}

customise it in order to place your error message in the right place (the adjacent td)

Ken Egozi
I have to use my own error messages(images with callout plugin). Therefor I am using showErrors option to use any thing in foor loop.Can you tell me that how can I show my own error messages/images with errorPlacement option.
NAVEED
I believe you can, instead of error.appendTo, do something like getMySpecialMarkupFrom(error).appendTo(wherever)
Ken Egozi
yes, it helped me. Thanks. Please see below for my answer.
NAVEED
A: 

After a time I got my answer. Infact I want to display an image with a callout(containing error) in next column(td) of input field that is not validated by validation plugin. And when an input field is validated, this error image should be removed with its callout on it.

Here is my solution.

$("form").validate({

 errorPlacement: function(error, element) {

  //There should be an error
  if(error.html() != ''){   

   element.parent().next().html("<img id='exclamation' src='images/exclamation.gif' />").callout({
    width  : 200,
    cornerRadius : 8,
    className : "validationCallout",
    content  : error,
    align  : "left",
    nudgeHorizontal : -14,
    nudgeVertical : 4,
    arrowHeight : 6
   });  
  }
 },

 success: function( label ) {
  $(obj).find(".valid").parent().next().html("");   //remove error image from next column(td) of input containing "valid" class
  $(obj).find(".valid").parent().next().closeCallout(); //remove callout on error image from next column(td) of input containing "valid" class
 }

});

This code may be complex but it is working for me now. A callout plugin is used here that is not related to the question but may help anyother. Can anyone make it more simple?

NAVEED
You might want to switch $(obj).find(".valid").parent().next().html(""); $(obj).find(".valid").parent().next().closeCallout(); with$(obj).find(".valid").parent().next() .html("") .closeCallout();
Ken Egozi
Thanks. Its good.
NAVEED