Skip to content

Instantly share code, notes, and snippets.

@danielgreen
Last active December 17, 2015 20:39
Show Gist options
  • Save danielgreen/5669443 to your computer and use it in GitHub Desktop.
Save danielgreen/5669443 to your computer and use it in GitHub Desktop.
AssertValue is a validation attribute (i.e. a data annotation) that can be applied to a property of a view model class. It causes the property to be invalid unless it has the value specified in the attribute's constructor. The attribute implements IClientValidatable which ties it up with client-side JavaScript in order to replicate the validatio…
// The following code may not work if executed after the page has loaded and the validator has initialised.
// That may require further investigation, but it certainly works if executed at the top of the page.
// Create the assertvalue validator method
jQuery.validator.addMethod("assertvalue", function (value, element, param) {
if (value.toLowerCase && param.toLowerCase)
return value.toLowerCase() == param.toLowerCase();
return value == param;
});
// Add the assertvalue adapter, tying in with the method just added and using the asserted parameter.
// These names must match the ones used by the server-side AssertValueAttribute.
jQuery.validator.unobtrusive.adapters.addSingleVal("assertvalue", "asserted");
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace Validators
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class AssertValueAttribute : ValidationAttribute, IClientValidatable
{
public object AssertedValue { get; protected set; }
public AssertValueAttribute(object value)
{
AssertedValue = value;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
bool match = false;
try
{
match = AssertedValue.Equals(value);
}
catch (Exception e) { }
if (match)
return ValidationResult.Success;
else
return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ErrorMessage = this.ErrorMessage,
ValidationType = "assertvalue"
};
rule.ValidationParameters.Add("asserted", AssertedValue);
yield return rule;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace ViewModels
{
public class SampleModel
{
[AssertValue(true, ErrorMessage = "This property should have the value 'true'")]
public bool MustBeTrue { get; set; }
[AssertValue(false, ErrorMessage = "This property should have the value 'false'")]
public bool MustBeFalse { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment