35 lines
1020 B
C#
35 lines
1020 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Web.Mvc;
|
|
|
|
namespace LeafWeb.WebCms.Controllers
|
|
{
|
|
// TODO http://stackoverflow.com/a/9036075/99492
|
|
public class EnforceTrueAttribute : ValidationAttribute, IClientValidatable
|
|
{
|
|
public override bool IsValid(object value)
|
|
{
|
|
if (value == null) return false;
|
|
if (value.GetType() != typeof(bool)) throw new InvalidOperationException("can only be used on boolean properties.");
|
|
return (bool)value;
|
|
}
|
|
|
|
public override string FormatErrorMessage(string name)
|
|
{
|
|
return "The " + name + " field must be checked in order to continue.";
|
|
}
|
|
|
|
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
|
|
{
|
|
yield return new ModelClientValidationRule
|
|
{
|
|
ErrorMessage =
|
|
string.IsNullOrEmpty(ErrorMessage)
|
|
? FormatErrorMessage(metadata.DisplayName)
|
|
: ErrorMessage,
|
|
ValidationType = "enforcetrue"
|
|
};
|
|
}
|
|
}
|
|
} |