Skip to content

Instantly share code, notes, and snippets.

@michaeljacobdavis
Created July 27, 2012 18:23
Show Gist options
  • Save michaeljacobdavis/3189574 to your computer and use it in GitHub Desktop.
Save michaeljacobdavis/3189574 to your computer and use it in GitHub Desktop.
Adding groups (1 message for multiple properties) to MVC3's Validation
$(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."
}
});
});
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();
}
}
.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