我创建了一个自定义ValidationAttribute来比较2个日期,并确保第二个日期大于第一个日期:
public sealed class IsDateAfter : ValidationAttribute, IClientValidatable
{
private readonly string testedPropertyName;
private readonly bool allowEqualDates;
public IsDateAfter(string testedPropertyName, bool allowEqualDates = false)
{
this.testedPropertyName = testedPropertyName;
this.allowEqualDates = allowEqualDates;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var propertyTestedInfo = validationContext.ObjectType.GetProperty(this.testedPropertyName);
if (propertyTestedInfo == null)
{
return new ValidationResult(string.Format("unknown property {0}", this.testedPropertyName));
}
var propertyTestedValue = propertyTestedInfo.GetValue(validationContext.ObjectInstance, null);
if (value == null || !(value is DateTime))
{
return ValidationResult.Success;
}
if (propertyTestedValue == null || !(propertyTestedValue is …Run Code Online (Sandbox Code Playgroud) 我正在使用Visual Studio 2012,我无法获得自定义属性客户端逻辑,以便以较小的规模重现,我创建了一个新的MVC 4项目,我创建了以下模型和属性,永远不会验证
public class MyModel
{
public int Id { get; set; }
[Required]
public string LastName { get; set; }
[NeverValid(ErrorMessage="Serverside Will Never Validate")]
public string FirstName { get; set; }
}
public class NeverValidAttribute : ValidationAttribute, IClientValidatable
{
public override bool IsValid(object value)
{
return false;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
return new ValidationResult(this.ErrorMessage, new[] { validationContext.MemberName });
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
yield return new ModelClientValidationRule
{
ErrorMessage …Run Code Online (Sandbox Code Playgroud) asp.net jquery jquery-validate unobtrusive-validation asp.net-mvc-4
我正在开发用于在 Visual Studio 2015 中进行验证的 ASP.NET MVC 5.2.3 自定义数据注释。它需要采用任意数量的字段并确保如果有一个值,它们都必须有一个值;如果它们都是空/空白,应该没问题。
一些例子有帮助:
但是,我不确定如何进行客户端验证,其中要验证的字段数量未知。
您如何使用接口GetClientValidationRules()方法的实现将其传递给客户端IClientValidatable?
另外,如何将这个新的数据注释应用到我的视图模型上的属性?会是这个样子吗?
[MultipleRequired("AppNumber", "UserId", /* more fields */), ErrorMessage = "Something..."]
[DisplayName("App #")]
public int AppNumber { get; set; }
[DisplayName("User ID")]
public int UserId { get; set; }
Run Code Online (Sandbox Code Playgroud)
这是我使用MultipleRequiredAttribute自定义数据注释类所能得到的:
public class MultipleRequiredAttribute : ValidationAttribute, IClientValidatable
{
private readonly string[] _fields;
public MultipleRequiredAttribute(params string[] fields)
{
_fields = fields;
}
protected override ValidationResult IsValid(object value, …Run Code Online (Sandbox Code Playgroud) 我创建了一个从ValidationAttribute派生的自定义验证器.我的undertsandng是它将为客户端脚本生成足够的元数据以自动验证(使用jquery.validate).自定义验证器在服务器端正常工作.但它不会在客户端激发错误消息.(其他默认验证器,如"StringLength"在客户端也正常工作.)我们如何纠正它?
public class Person
{
[Required(ErrorMessage = "First name required")]
public string FirstName { get; set; }
[CustomStartLetterMatch("FirstName")]
[StringLength(5,ErrorMessage = "Must be under 5 characters")]
public string LastName { get; set; }
[Range(18,50,ErrorMessage="Must be between 18 and 50")]
public int Age { get; set; }
}
public sealed class CustomStartLetterMatch : ValidationAttribute
{
private const string _defaultErrorMessage = " First letter of '{0}' must be same as first letetr of '{1}'";
private string _basePropertyName;
public CustomStartLetterMatch(string basePropertyName)
: base(_defaultErrorMessage)
{ …Run Code Online (Sandbox Code Playgroud)