Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 545 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Enforcing a model's boolean value to be true using data annotations

#1
Simple problem here (I think).

I have a form with a checkbox at the bottom where the user must agree to the terms and conditions. If the user doesn't check the box, I'd like an error message to be displayed in my validation summary along with the other form errors.

I added this to my view model:

[Required]
[Range(1, 1, ErrorMessage = "You must agree to the Terms and Conditions")]
public bool AgreeTerms { get; set; }

But that didn't work.

Is there an easy way to force a value to be true with data annotations?
Reply

#2
You can write a custom validation attribute which has already been mentioned. You will need to write custom javascript to enable the unobtrusive validation functionality to pick it up if you are doing client side validation. e.g. if you are using jQuery:

// extend jquery unobtrusive validation
(function ($) {

// add the validator for the boolean attribute
$.validator.addMethod(
"booleanrequired",
function (value, element, params) {

// value: the value entered into the input
// element: the element being validated
// params: the parameters specified in the unobtrusive adapter

// do your validation here an return true or false

});

// you then need to hook the custom validation attribute into the MS unobtrusive validators
$.validator.unobtrusive.adapters.add(
"booleanrequired", // adapter name
["booleanrequired"], // the names for the properties on the object that will be passed to the validator method
function(options) {

// set the properties for the validator method
options.rules["booleanRequired"] = options.params;

// set the message to output if validation fails
options.messages["booleanRequired] = options.message;

});

} (jQuery));

Another way (which is a bit of a hack and I don't like it) is to have a property on your model that is always set to true, then use the **CompareAttribute** to compare the value of your **AgreeTerms ** attribute. Simple yes but I don't like it :)
Reply

#3
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace Checked.Entitites
{
public class BooleanRequiredAttribute : ValidationAttribute, IClientValidatable
{
public override bool IsValid(object value)
{
return value != null && (bool)value == true;
}

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
//return new ModelClientValidationRule[] { new ModelClientValidationRule() { ValidationType = "booleanrequired", ErrorMessage = this.ErrorMessage } };
yield return new ModelClientValidationRule()
{
ValidationType = "booleanrequired",
ErrorMessage = this.ErrorMessageString
};
}
}
}
Reply

#4
There's actually a way to make it work with DataAnnotations. The following way:


[Required]
[Range(typeof(bool), "true", "true")]
public bool AcceptTerms { get; set; }
Reply

#5
ASP.Net Core 3.1

I know this is a very old question but for asp.net core the `IClientValidatable` does not exist and i wanted a solution that works with `jQuery Unobtrusive Validation` as well as on server validation so with the help of this SO question [Link](

[To see links please register here]

) i made a small modification that works with boolean field like checkboxes.

# Attribute Code
```
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class MustBeTrueAttribute : ValidationAttribute, IClientModelValidator
{
public void AddValidation(ClientModelValidationContext context)
{
MergeAttribute(context.Attributes, "data-val", "true");
var errorMsg = FormatErrorMessage(context.ModelMetadata.GetDisplayName());
MergeAttribute(context.Attributes, "data-val-mustbetrue", errorMsg);
}

public override bool IsValid(object value)
{
return value != null && (bool)value == true;
}

private bool MergeAttribute(
IDictionary<string, string> attributes,
string key,
string value)
{
if (attributes.ContainsKey(key))
{
return false;
}
attributes.Add(key, value);
return true;
}

}
```
# Model
```
[Display(Name = "Privacy policy")]
[MustBeTrue(ErrorMessage = "Please accept our privacy policy!")]
public bool PrivacyPolicy { get; set; }
```

# Client Side Code
```
$.validator.addMethod("mustbetrue",
function (value, element, parameters) {
return element.checked;
});

$.validator.unobtrusive.adapters.add("mustbetrue", [], function (options) {
options.rules.mustbetrue = {};
options.messages["mustbetrue"] = options.message;
});
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through