views:

192

answers:

1

I'm trying to place separate error messages in separate elements when validating a large form. The form is divided into jQueryUI tabs, then accordions.

When there is an error in an element of a tab, I want to append a red exclamation point to the name of the tab, and if the error is in an accordion element, I also want to append the red exclamation point to the name of the accordion element.

Subsequently, when the errors are corrected, I would like those red ! to be removed (exactly as the error message beneath the invalid field is removed.

So:

Tab1 Tab2 Accordion1 Accordion2 Tab3

If the elements in accordion 2 have an error, I want to append a red ! to accordion2 and tab2:

Tab1 Tab2! Accordion1 Accordion2! Tab3

Then remove when the elements successfully validate. I've been trying forever, but I can't figure out how to conditionally change the errorElement (a label won't work for the tab and accordion, but is perfect for the actual element)...

Hopefully this makes sense, and thanks for any input you can provide.

+1  A: 

You should try using the errorcontainer option

$("#myform").validate({
   errorContainer: "#Accordion2"
})

Edited

$("#myform").validate({
   showErrors: function(errorMap, errorList) {
       //error define where the errors in the ErrorList Go
       //you could also try
       this.errorContainer = "element1id, element2id"
   }
})

Edited2

Note: I am not testing this code. just providing you ideas.

$("#myform").validate({
   errorPlacement: function(error, element) {
        error.appendTo( element.closest('.tab'));

   }
})
John Hartsock
Reading comprehension for the win. Ok. I'm testing now.
Andy Poquette
So, this doesn't quite work, because I need it to dynamically determine which top level element the errors are in and add the ! to that element. This will add the errors to each element even if there aren't errors in THAT element.
Andy Poquette
ok well specify the errorcontainer when you showErrors. See Edited
John Hartsock
actually using the errorplacement option would probably be better
John Hartsock
Is there a way to count the number of errors contained in a certain element? For instance if I have nested <div>s and there are errors two levels down, can I just get the total number of errors from the top level <div>?The error placement function works, but only 1/2 way because I need the error to also remain in a label form in the element(which I'm currently using the errorPlacement function for)Also, thank you for all of your suggestions and help.
Andy Poquette
It seems like if I can edit the numberOfInvalids() function to accept an element to count through, I could makes this happen. Appending an error only the field I choose where numberOfInvalids() > 0...I'll work on it.
Andy Poquette
I can't seem to figure out how to check a specific element (and all of it's children) for errors, then place an error message specifically tied to that information while leaving the defaults all in place.I'll keep working!
Andy Poquette
hey dont modify the errors placement. But instead under the show errors events get the number of invalid errors mark the tab with a red asterisk and ([numberofErrors]). then call defaultShowErrors
John Hartsock
I tried that, but I can't figure out how to get:[numberOfErrors] in <element> I can only get them for the entire form...
Andy Poquette
try this $('input.errorClass, select.errorClass, textarea.errorClass', $('#yourTab')).length where errorClass is the class added when the element is invalid. Note you will need to modify that a bit to handle radio buttons properly as 1 radio group really only counts for 1 errored element.
John Hartsock
I tried that, and I can get it to work if I set that selector as a timeout, but I can't make it work inherently with the validation. I need to study the plugin more and see what I can do. Maybe contact the author and see if he can add another method that lets you specify secondary error placement conditionally.Thank you for your help John.
Andy Poquette