用于验证模型的数据注释,我如何验证它以便日期不在未来?
DataAnotation to validate a model, how do I validate it so that the date is not in the future?
我有审核模型,我正在尝试验证该模型,以便
当用户选择日期时,不能是将来的日期。
Review.cs
public class Review : BaseEntity{
[Key]
public int Id {get; set;}
[Required(ErrorMessage="You need a restaurant name!")]
public string RestaurantName {get; set;}
[What do I put in here??]
public DateTime Date {get; set;}
}
我是新手,documentation有点难懂
非常感谢您的提前帮助。
您需要为此编写自定义验证器。您可以随意命名,例如。
[DateNotInTheFuture]
public DateTime Date { get; set; }
流程本身在这篇文章中有详细的解释:https://msdn.microsoft.com/en-us/library/cc668224.aspx
总结:
- 创建继承 ValidationAttribute
的新密封 public class
- 在 class 中实现 IsValid 方法的重写。
- 在 IsValid 方法和 return 中编写您的自定义验证逻辑
您可以创建一个自定义验证属性来执行您的自定义逻辑,并使用它来修饰您的 属性 名称。
public class DateLessThanOrEqualToToday : ValidationAttribute
{
public override string FormatErrorMessage(string name)
{
return "Date value should not be a future date";
}
protected override ValidationResult IsValid(object objValue,
ValidationContext validationContext)
{
var dateValue = objValue as DateTime? ?? new DateTime();
//alter this as needed. I am doing the date comparison if the value is not null
if (dateValue.Date > DateTime.Now.Date)
{
return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
}
return ValidationResult.Success;
}
}
现在在您的视图模型中,用这个新的自定义属性装饰您的 属性 名称
[DateLessThanOrEqualToToday]
public DateTime Date { get; set; }
此自定义验证属性主要关注您的特定验证逻辑。您可以根据需要更改它以包含更多空值检查、最小值检查等。
使用属性尝试自定义验证或标准模型验证。
第一个选项,在 属性 标准数据注释验证上设置属性:
- 设置带有错误消息属性和日期格式字符串的 DateType 属性。
- 如果需要,请设置 Range 属性。
设置 Display 属性以在屏幕上显示标签。
[DataType(DataType.Date), ErrorMessage = "Please enter a correct date format dd/mm/yyyy hh:mm", DisplayFormat( DataFormatString="{0:dd/MM/yyyy}", ApplyFormatInEditMode=true )]
[Range(typeof(DateTime), "1/1/2016", "1/1/2011")]
[Display(Name = "My Date")]
public DateTime Date {get; set;}
第二个选项,自定义验证方式:
您必须扩展 ValidationAttribute class 并覆盖 IsValid
:
public class MyDateValidation: ValidationAttribute
{
public override bool IsValid(object value)
{
// check your business date property
DateTime myDatetime;
bool isParsed = DateTime.TryParse((string)value, out myDatetime);
if(!isParsed)
return false;
return true;
}
}
[MyDateValidation(ErrorMessage="Your message")]
public Datetime myDate { get; set; }
查看我关于此主题的其他 。
我有审核模型,我正在尝试验证该模型,以便 当用户选择日期时,不能是将来的日期。
Review.cs
public class Review : BaseEntity{
[Key]
public int Id {get; set;}
[Required(ErrorMessage="You need a restaurant name!")]
public string RestaurantName {get; set;}
[What do I put in here??]
public DateTime Date {get; set;}
}
我是新手,documentation有点难懂
非常感谢您的提前帮助。
您需要为此编写自定义验证器。您可以随意命名,例如。
[DateNotInTheFuture]
public DateTime Date { get; set; }
流程本身在这篇文章中有详细的解释:https://msdn.microsoft.com/en-us/library/cc668224.aspx
总结:
- 创建继承 ValidationAttribute 的新密封 public class
- 在 class 中实现 IsValid 方法的重写。
- 在 IsValid 方法和 return 中编写您的自定义验证逻辑
您可以创建一个自定义验证属性来执行您的自定义逻辑,并使用它来修饰您的 属性 名称。
public class DateLessThanOrEqualToToday : ValidationAttribute
{
public override string FormatErrorMessage(string name)
{
return "Date value should not be a future date";
}
protected override ValidationResult IsValid(object objValue,
ValidationContext validationContext)
{
var dateValue = objValue as DateTime? ?? new DateTime();
//alter this as needed. I am doing the date comparison if the value is not null
if (dateValue.Date > DateTime.Now.Date)
{
return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
}
return ValidationResult.Success;
}
}
现在在您的视图模型中,用这个新的自定义属性装饰您的 属性 名称
[DateLessThanOrEqualToToday]
public DateTime Date { get; set; }
此自定义验证属性主要关注您的特定验证逻辑。您可以根据需要更改它以包含更多空值检查、最小值检查等。
使用属性尝试自定义验证或标准模型验证。
第一个选项,在 属性 标准数据注释验证上设置属性:
- 设置带有错误消息属性和日期格式字符串的 DateType 属性。
- 如果需要,请设置 Range 属性。
设置 Display 属性以在屏幕上显示标签。
[DataType(DataType.Date), ErrorMessage = "Please enter a correct date format dd/mm/yyyy hh:mm", DisplayFormat( DataFormatString="{0:dd/MM/yyyy}", ApplyFormatInEditMode=true )] [Range(typeof(DateTime), "1/1/2016", "1/1/2011")] [Display(Name = "My Date")] public DateTime Date {get; set;}
第二个选项,自定义验证方式:
您必须扩展 ValidationAttribute class 并覆盖 IsValid
:
public class MyDateValidation: ValidationAttribute
{
public override bool IsValid(object value)
{
// check your business date property
DateTime myDatetime;
bool isParsed = DateTime.TryParse((string)value, out myDatetime);
if(!isParsed)
return false;
return true;
}
}
[MyDateValidation(ErrorMessage="Your message")]
public Datetime myDate { get; set; }
查看我关于此主题的其他