By default, the plugin inserts a <label>
element, as per the errorElement
option, containing the error message and then toggles this <label>
element as needed. If your code worked, it would over-ride this errorElement
and the plugin would no longer be able to find and toggle the messages.
"How can I stop this?"
You can't. The developer provides no direct option or method of adding any markup to the plain text that is contained within the validation message element.
However, there are two workarounds that will achieve your desired markup:
ONE: You could set custom messages that contain the <i>
already attached.
$('form').validate({
errorElementClass: 'input-validation-error',
errorClass: 'field-validation-error',
rules: {
foo: {
required: true
},
bar: {
required: true
}
},
messages: {
foo: {
required: "<i class='icon-warning'></i> This field is required"
},
bar: {
required: "<i class='icon-warning'></i> This field is required"
}
},
....
This solution is very tedious if your form contains a lot of fields and rules.
Proof-of-concept DEMO 1: jsfiddle.net/g4zq7p8j/
You could also use jQuery .extend()
to change all the default messages at once
TWO: Otherwise, manually put the <i>
elements into your markup and set them as display: none
within your CSS.
<input type="text" name="foo" /><i class="icon-warning"></i>
Then you can use the highlight
and unhighlight
callback options to toggle these <i>
elements along with the associated error messages.
$('form').validate({
errorElementClass: 'input-validation-error',
errorClass: 'field-validation-error',
errorPlacement: function(error, element) {
error.insertAfter($(element).next('i'));
},
highlight: function(element, errorClass, validClass) {
.... // default highlight code here
$(element).next('i').show();
},
unhighlight: function(element, errorClass, validClass) {
.... // default unhighlight code here
$(element).next('i').hide();
},
....
Proof-of-concept DEMO 2: jsfiddle.net/kxmw9do2/
You could also easily have jQuery insert these elements dynamically on page load
$(document).ready(function() {
$('<i class="icon-warning"></i>').insertAfter('[type="text"]');
....