I need some advice on boolen validation. I read similar topics, and currently have done following:
In Model
[MustBeTrue(ErrorMessage = "You must accept the terms and conditions")]
[Display(ResourceType = typeof(ValRes.ValidationStrings), Name = "Accept_general_conditions")]
public bool Accept_general_conditions { get; set; }
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class MustBeTrueAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
return value != null && value is bool && (bool)value;
}
}
Do you have a Html.ValidationMessage() for the bool in the view?
Also, you seems to be conflating client-side validation and server-side validation. From what you've shown, I would expect server side validation to work and if you have the Html.ValidationMessage in the page then you should see it. But you don't automatically
get client-side validation. If you want client-side validation you need your attribute to implement IClientValidatable and then you need to write some custom jquery.validate validation login inside of a validation method. Brad Wilson
goes into it a bit.
addapter are used with you add a new method to jquery validation. when you add a new method to the validate, you also add an adapter that can read the attributes, and produce the rule and its parameters. see the jquery validaiton doc for writing methods
and rules. see the unobtrusive source for how to create and use an adapter.
kvh
Member
131 Points
108 Posts
Validating boolean field to be true
Mar 16, 2012 07:18 PM|LINK
I need some advice on boolen validation. I read similar topics, and currently have done following:
In Model
[MustBeTrue(ErrorMessage = "You must accept the terms and conditions")]
[Display(ResourceType = typeof(ValRes.ValidationStrings), Name = "Accept_general_conditions")]
public bool Accept_general_conditions { get; set; }
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class MustBeTrueAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
return value != null && value is bool && (bool)value;
}
}
in the View I have references
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
but for some reason error is not given. any ideas what could be missing?
thanks.
BrockAllen
All-Star
27534 Points
4907 Posts
MVP
Re: Validating boolean field to be true
Mar 16, 2012 07:54 PM|LINK
Do you have a Html.ValidationMessage() for the bool in the view?
Also, you seems to be conflating client-side validation and server-side validation. From what you've shown, I would expect server side validation to work and if you have the Html.ValidationMessage in the page then you should see it. But you don't automatically get client-side validation. If you want client-side validation you need your attribute to implement IClientValidatable and then you need to write some custom jquery.validate validation login inside of a validation method. Brad Wilson goes into it a bit.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
kvh
Member
131 Points
108 Posts
Re: Validating boolean field to be true
Mar 16, 2012 09:15 PM|LINK
thanks :) I added @Html.ValidationMessageFor(model => model.Accept_general_conditions) and from Model I now correctly get the validation message :)
but having some small issues still understanding the JQuery for client-side. I added on the page
<script type="text/javascript">
$(function () {
jQuery.validator.unobtrusive.adapters.addBool("Accept_general_conditions");
});
</script>
but this doesn't fire. what should be done to get that also working?
bruce (sqlwo...
All-Star
36852 Points
5446 Posts
Re: Validating boolean field to be true
Mar 17, 2012 03:36 AM|LINK
addapter are used with you add a new method to jquery validation. when you add a new method to the validate, you also add an adapter that can read the attributes, and produce the rule and its parameters. see the jquery validaiton doc for writing methods and rules. see the unobtrusive source for how to create and use an adapter.
BrockAllen
All-Star
27534 Points
4907 Posts
MVP
Re: Validating boolean field to be true
Mar 17, 2012 04:29 PM|LINK
You then have to implement a jQuery validation "method" called "Accept_general_conditions". It would be something like this:
$.validator.addMethod("Accept_general_conditions",
function (value, element) {
if (this.optional(element)) {
return true;
}
return value == true;
}, "You must agree to the terms."
);
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/