views:

77

answers:

1

I've searched and read (not just on SO) but can't seem to find a solution for this issue. Basically, what is happening is that the jQuery validate plugin is (according to documentation) supposed to hide the errorContainer that is set in its configuration. Unfortunately, it's not hiding it, it's just removing the contents.

Please take a look at the page I'm working on. (The form is not doing anything at the moment, other than submitting to itself).

In order to reproduce the issue, just enter a non-email value in the the field and click somewhere else on the page. You should see the error message appear. Now go in and delete the values, and watch what happens. The error message disappears but the container is not hidden, and since it is styled (border/background/etc) it can still be seen even after the error has been corrected.

The relevant CSS for this div:

div#mailform div#error {
     font-size: 10px;
     width: 288px;
     text-align: center;
     display: inline;
     float: left;
     position: fixed;
     z-index: 1;
     padding: 2px;
     margin-left: 6px;
     background-color: #fcd6cf;
     border: 1px solid #bb1124;
     display: none;
}

The jQuery I'm using, including another function I'm using to deal with the field's default value (already tested disabling the other function to see if that was interfering with the show/hide):

$.fn.search = function() {
 return this.focus(function() {
  if( this.value == this.defaultValue ) {
   this.value = "";
  }
 }).blur(function() {
  if( !this.value.length ) {
   this.value = this.defaultValue;
  }
 });
};

$("#email").search();


$("#subscribeform").validate({
 errorContainer: "#error",
 errorLabelContainer: "#error",
 errorClass: "invalid",
 rules: {
  email: {
   email: true
  },
 },
 messages: {
  name: "Please specify your name",
  email: {
    email: "Please enter a valid email address."
  }
 }
});

According to the validate plugin documentation at: http://docs.jquery.com/Plugins/Validation/validate#toptions, the errorContainer parameter "Uses an additonal container for error messages. The elements given as the errorContainer are all shown and hidden when errors occur." (My emphasis). But that's obviously not happening!

So I'm puzzled, and eager to find out what's going wrong with my code so that I can get past this and whip up the PHP side of it. Thanks in advance for reading and for any help you guys can give.

+1  A: 

What you're doing is inserting the error element into a pre-defined div. The error element is shown and hidden, but not the error element's parent. Try this instead:

HTML:

<form id="subscribeform" name="subscribeform" action="mailform.php" method="post">
    <div id="mailform">
        <div id="desc">Stay informed with the latest news from the Foundation: </div>     
        <input type="text" id="email" name="email" placeholder="Enter email address" value="Enter email address"/>
        <input type="image" src="images/sendbutton.jpg" id="sendbutton" name="sendbutton" />
    </div>
</form>

CSS:

div.error { /* the .error class is automatically assigned by the plug-in */
     font-size: 10px;
     width: 288px;
     text-align: center;
     display: inline;
     float: left;
     position: fixed;
     z-index: 1;
     padding: 2px;
     margin-left: 6px;
     background-color: #fcd6cf;
     border: 1px solid #bb1124;
     display: none;
}

And finally the jQuery:

$("#subscribeform").validate({
    errorElement: "div",
    errorPlacement: function(error, element) {
        error.insertBefore($("#desc"));
    },
    rules: {
        email: {
            email: true
        },
    },
    messages: {
        name: "Please specify your name",
        email: {
            email: "Please enter a valid email address."
        }
    }
});

Hope this helps !


EDIT

Here's a live JSFiddle example: http://jsfiddle.net/SvWJ3/

FreekOne
Thanks FreekOne, it gives me a direction to look in, but unfortunately it doesn't solve the problem. If you'll change the URL to go to the mailform2.php page, you'll see what happens when I implement your suggestions. Even though you specified the errorContainer as a div, it's not a div that's created with the error class, but rather a label tag. Where do we go from there?
helios
Please copy the jQuery part of my answer again. I messed it up initially (I forgot to change `errorContainer` to `errorElement`) but it is now fixed and should work properly.
FreekOne
Actually, I must have copied your jQuery before you edited it, since I see you changed a line (error.insertBefore($("#desc"));). I also had trouble copying from jsfiddle, it inserted some weird special characters which broke the jQuery. However, when I re-typed it as you had it, it worked! Thank you very much. Now I have to figure out *why* it worked, but I'll save that for after the project is done. Thanks again!
helios
By the way, JSFiddle is amazing, thanks for the tip!
helios
Glad to be of help ! :) The reason it didn't work initially was because the plugin creates its own error element (as you've already discovered, a `label` by default) which is then inserted into the DOM (right before the invalid element, by default). Basically, the error element was inserted in your pre-defined `div` and then hidden when the field validated, but the pre-defined `div` itself was not. So, the solution was to change the error element to a `div` and simply style it using the class you've already created. Hope that makes sense.
FreekOne
Also, the line you've mentioned is meant to insert the error element exactly where you had it initially (as the first child of the `mailform` `div`) and it literally means "insert the error before the element that has `desc` as id", as opposed to the initial code which would insert it as the last child of the `mailform` `div` i.e. append it to the element's `.parent()`.
FreekOne
Cool, that makes sense and teaches me a lot about how this works. Do you know off the top of your head if the validate plugin offers any hooks for animating the show/hide of the error? I'd like to make the div have a slight bounce when it appears, but I can't seem to find a way to hook that animation into the validate plugin. (Maybe the animation is a bad idea anyway, would be neat to see the error "pop" though. :-D )
helios
It would be fairly easy to combine the validator's built-in `highlight` and `unhighlight` functions with any jQuery animation method. See my answer on my own [SO question](http://stackoverflow.com/questions/2505053/jquery-validation-plugin-add-remove-class-to-from-elements-error-container) for more info on how to use these functions. Took me a while to find them :)
FreekOne
Thanks, and thanks for answering my follow-up question (I read on one of your comments to that question that this is normally done by asking another question and then linking to the old one). I just figured since you seemed to know a lot about the validation plugin, you might know about those hooks as well (and I was right :)). Cheers!
helios
Glad to be of help :)
FreekOne