views:

418

answers:

3

Hi, It's my first time using StackOverFlow and first time trying to set up jQuery Validation. It's displaying <label> tags with the error messages as default behaviour, however the way my CSS is set up I need a div to wrap around the offending element and a message display in <p> tags.

Without errors, my html looks like this:

<div class="grid-26 append-2">
    <p class="noMarginBottom">
     <label>First Name</label>
     <div class="jNiceInputWrapper">
      <div class="jNiceInputInner">
       <input type="text" class="text jNiceInput" name="name"/>
      </div>
     </div>
    </p>         
    <span class="clear"/>
</div>

And with Errors, it needs to look like this - Note the div with class "error" and the <p> tag.

<div class="grid-26 append-2">
    <div class="error">
     <p>Please write your real name</p>
     <p class="noMarginBottom">
      <label>First Name</label>
      <div class="jNiceInputWrapper">
       <div class="jNiceInputInner">
        <input type="text" class="text jNiceInput" name="name"/>
       </div>
      </div>
     </p>         
     <span class="clear"/>
    </div>
</div>

My Validation code is very basic.

$(document).ready(function(){
    $("#contact_form").validate({
     rules:{
      name: {
       required: true
      }
     }
    });
});

This is my first venture into jQuery and form validation, so I'll be the first to say "I'm lost!" any help would be greatly appreciated. Thanks.

A: 
label.parents('.append-2')
     .prepend('<p>Please write your real name</p>')
     .wrapInner('<div class="error"></div>');

To restore it to its previous state:

var error = label.parents('.error');
error.siblings().remove();
error.replaceWith(error.children());
Alex Barrett
Thanks for that! It looks like it's going to be simpler than I though. However, I forgot to mention, there are many append-2 divs on the page. How can this code be modified so it walks up from the label? for instance `if label == name { parent.wrap('<div class="error></div>'); or something?I just found out about the errorPlacement method for validation.. is this what I need to use?
Samuurai
I'm afraid I'm not familiar with the particular jQuery plugin you're using, so I can't comment on the `errorPlacement` method it offers - hopefully you will find it useful though. I've updated the answer to show you how to walk up from _any_ element inside the `div`, including the `label`.
Alex Barrett
Great! I looked up some documentation to modify just one thing..Since there are lots of labels on the page, I made it look for the attribute, so I did $('[for=name]').parents.......Thanks a lot for your help!
Samuurai
Feel cheeky asking, but do you know what code I need to use to remove these?
Samuurai
Try that. I've not tested, so if it doesn't work let me know :)
Alex Barrett
A: 

I figured it out:

     ,errorPlacement: function(error,element){

  $('[for='+element.attr("name")+']').parents('.grid-26')

  .prepend('<p>'+error.text()+'</p>')

  .wrapInner('<div class="error"></div>');

 }
Samuurai
A: 

Here's my full working code- I opted to use the default <label> tags rather than the <p> tags.

$(document).ready(function(){
$("#contact_form").validate({
 rules:{
  name: {
   required: true
  },
 }
 ,messages: {
  name: "Please write your full name"
 }
 ,errorClass: "error_label"
 ,errorPlacement: function(error,element){
  parentDiv = element.parents('.grid-26');
  parentDiv.wrapInner('<div class="error"></div>');
  errorDiv = parentDiv.children(".error");
  error.appendTo(errorDiv);
 }
 ,success: function(element){
  var errorDiv = element.parents('.error');
  element.remove();
  errorDiv.siblings().remove();
  errorDiv.replaceWith(errorDiv.children());
 }

});
});

Thanks for your help Alex!!

Samuurai