8

Using MVC2, I have a simple ViewModel that contains a bool field that is rendered on the view as a checkbox. I would like to validate that the user checked the box. The [Required] attribute on my ViewModel doesn't seem to do the trick. I believe this is because the unchecked checkbox form field is not actually transmitted back during the POST, and therefore the validation doesn't run on it.

Is there a standard way to handle checkbox "required" validation in MVC2? or do I have to write a custom validator for it? I suspect the custom validator won't get executed either for the reason mentioned above. Am I stuck checking for it explicitly in my controller? That seems messy...

Any guidance would be appreciated.

Scott

EDIT FOR CLARITY: As pointed out in comments below, this is a "agree to our terms" type of checkbox, and therefore "not checked" is a valid answer, so I'm really looking for an "is checked" validation.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Scott Mayfield
  • 2,254
  • 2
  • 20
  • 28
  • 1
    Is this an "I accept the terms of the license agreement" type of checkbox? The [Required] attribute doesn't work because an unchecked checkbox is a valid value (false). – Robert Harvey May 04 '10 at 16:21
  • Yes, it is exactly that, and if they don't check it, we're just redisplaying the same view with a validation message that you must accept the terms to continue. I see your point about false being "valid" though. – Scott Mayfield May 05 '10 at 00:18
  • The answer here has a nice way of doing it that works with the client validators: [http://stackoverflow.com/questions/4934032/mvc3-make-checkbox-required-via-jquery-validate](http://stackoverflow.com/questions/4934032/mvc3-make-checkbox-required-via-jquery-validate) – Chris.ZA Aug 14 '12 at 15:30

4 Answers4

14

a custom validator is the way to go. I'll post my code which I used to validate that the user accepts the terms ...

public class BooleanRequiredToBeTrueAttribute : RequiredAttribute
{
    public override bool IsValid(object value)
    {
        return value != null && (bool)value;
    }
}
SQueek
  • 617
  • 4
  • 8
  • Does this validate client side as well? – jrummell Feb 22 '11 at 20:22
  • not automatically as i know. In order to create client side validation you have to create an validator for the custom validation. see following link for details on how to implement this: http://www.highoncoding.com/Articles/729_Creating_Custom_Client_Side_Validation_in_ASP_NET_MVC_2_0.aspx You have to create a DataAnnotationsModelValidator for the custom validation – SQueek Feb 23 '11 at 10:25
12

I usually use:

[RegularExpression("true")]
Jokin
  • 4,188
  • 2
  • 31
  • 30
  • 2
    I like this one, though I had to tweak it this: `[RegularExpression("true|True")]` to get it work for me. (I'm using MVC3, if that makes a difference?) – Merenzo Nov 21 '11 at 05:11
  • did it give you client side validation as well ? got the server side correctly – Shashank Shekhar Nov 29 '11 at 10:19
6

If you didn't want to create your own custom validator and still wanted to use existing attributes in the model you could use:

[Range(typeof(bool), "true", "true", ErrorMessage="You must accept the terms and conditions.")]

This ensures that the range of the boolean value is between true and true. However, whilst this method will work, I would still prefer to use a custom validator in this scenario. I just thought i'd mention this as an alternative option.

Dangerous
  • 4,818
  • 3
  • 33
  • 48
0

I too am looking for a way to have the model binder correctly handle check boxes with Boolean values. In the mean time I'm using this in the Actions:

Object.Property = !String.IsNullOrEmpty(Request.Form["NAME"]);

Maybe this will be of some use to you.

Gup3rSuR4c
  • 9,145
  • 10
  • 68
  • 126