Created
July 27, 2012 18:23
-
-
Save michaeljacobdavis/3189574 to your computer and use it in GitHub Desktop.
Adding groups (1 message for multiple properties) to MVC3's Validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function () { | |
$("form").validate().groups = (function () { | |
var result = {}; | |
$('form .group').each(function (i) { | |
$(this).find('input').each(function () { | |
result[$(this).attr('name')] = 'groupname' + i; | |
}); | |
}); | |
return result; | |
})(); | |
$("#First").rules("add", { | |
required: true, | |
messages: { | |
required: "Please enter an occupation." | |
} | |
}); | |
$("#Last").rules("add", { | |
required: true, | |
messages: { | |
required: "Please enter an occupation." | |
} | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onError(error, inputElement) { // 'this' is the form element | |
var container = $(this).find("[data-valmsg-for='" + inputElement[0].name + "']"), | |
replace = $.parseJSON(container.attr("data-valmsg-replace")) !== false; | |
container.removeClass("field-validation-valid").addClass("field-validation-error"); | |
error.data("unobtrusiveContainer", container); | |
if (replace) { | |
container.empty(); | |
error.removeClass("input-validation-error").appendTo(container); | |
} | |
else { | |
error.hide(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.input-validation-error | |
{ | |
border: 1px solid #ff0000; | |
background-color: #ffeeee; | |
} | |
span.input-validation-error { | |
border: none; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment