I am using DataAnnotations
to specify my validation rules and by default, these validation rules are added on the client side for them to be validated by jquery.
I'd like to use BootstrapValidator.js since I like the way the error/success messages get rendered. However, it requires me to redefine the validation rules on the client side. An article about BootstrapValidator.js can be found.
Is there a way I can use DataAnnotations
and define the rules in a single place and still use BootstrapValidator?
Any thoughts?
In ASP.NET MVC, Data Annotation is used for data validation for developing web-based applications. We can quickly apply validation with the help of data annotation attribute classes over model classes.
Answers. You have added the Assessment type as a parameter in the post action method. Hence MVC try to create a new instance of Assessment class. If the value for Title field is not provided in the view, then it will try to set the value for Title property as null.
In ASP.NET MVC application we can do the Server Side Validation on the Model using Data Annotations. Data Annotations is a namespace that provides attribute classes, and these classes define metadata for controls. In MVC we decorate Model Properties with these attribute classes to provide metadata.
No need to redefine validation rules again. You can simply integrate MVC type validation (which is Jquery Validation Plugin) with Bootstrap styling by dropping a quick script and reference it after your MVC validation scripts:
$(function () {
$('span.field-validation-valid, span.field-validation-error').each(function () {
$(this).addClass('help-inline');
});
$('.validation-summary-errors').each(function () {
$(this).addClass('alert');
$(this).addClass('alert-error');
$(this).addClass('alert-block');
});
$('form').submit(function () {
if ($(this).valid()) {
$(this).find('div.control-group').each(function () {
if ($(this).find('span.field-validation-error').length == 0) {
$(this).removeClass('error');
}
});
}
else {
$(this).find('div.control-group').each(function () {
if ($(this).find('span.field-validation-error').length > 0) {
$(this).addClass('error');
}
});
$('.validation-summary-errors').each(function () {
if ($(this).hasClass('alert-error') == false) {
$(this).addClass('alert');
$(this).addClass('alert-error');
$(this).addClass('alert-block');
}
});
}
});
$('form').each(function () {
$(this).find('div.control-group').each(function () {
if ($(this).find('span.field-validation-error').length > 0) {
$(this).addClass('error');
}
});
});
$("input[type='password'], input[type='text']").blur(function () {
if ($(this).hasClass('input-validation-error') == true || $(this).closest(".control-group").find('span.field-validation-error').length > 0) {
$(this).addClass('error');
$(this).closest(".control-group").addClass("error");
} else {
$(this).removeClass('error');
$(this).closest(".control-group").removeClass("error");
}
});
});
var page = function () {
//Update that validator
$.validator.setDefaults({
highlight: function (element) {
$(element).closest(".control-group").addClass("error");
},
unhighlight: function (element) {
$(element).closest(".control-group").removeClass("error");
}
});
} ();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With