我有以下规则
第一个使用不显眼的客户端验证工作,第二个没有
任何想法为什么?
RuleFor(x => x.StartDate)
.LessThanOrEqualTo(x => x.EndDate.Value)
.WithLocalizedMessage(() => CommonRes.Less_Than_Or_Equal_To, filters => CommonRes.Start_Date, filters => CommonRes.End_Date);
RuleFor(x => x.StartDate)
.GreaterThanOrEqualTo(x => x.AbsoluteStartDate)
.LessThanOrEqualTo(x => x.AbsoluteEndDate)
.WithLocalizedMessage(() => CommonRes.Between, filters => CommonRes.Start_Date, filters => filters.AbsoluteStartDate, filters => filters.AbsoluteEndDate);
Run Code Online (Sandbox Code Playgroud) 有一个表单,用户可以在其中输入事件的开始日期/时间和结束日期/时间.到目前为止,这是验证器:
public class EventModelValidator : AbstractValidator<EventViewModel>
{
public EventModelValidator()
{
RuleFor(x => x.StartDate)
.NotEmpty().WithMessage("Date is required!")
.Must(BeAValidDate).WithMessage("Invalid date");
RuleFor(x => x.StartTime)
.NotEmpty().WithMessage("Start time is required!")
.Must(BeAValidTime).WithMessage("Invalid Start time");
RuleFor(x => x.EndTime)
.NotEmpty().WithMessage("End time is required!")
.Must(BeAValidTime).WithMessage("Invalid End time");
RuleFor(x => x.Title).NotEmpty().WithMessage("A title is required!");
}
private bool BeAValidDate(string value)
{
DateTime date;
return DateTime.TryParse(value, out date);
}
private bool BeAValidTime(string value)
{
DateTimeOffset offset;
return DateTimeOffset.TryParse(value, out offset);
}
}
Run Code Online (Sandbox Code Playgroud)
现在我还想添加EndDateTime> StartDateTime(组合日期+时间属性)的验证,但不知道如何去做.
编辑: 为了澄清,我需要以某种方式结合EndDate + EndTime/StartDate + StartTime即DateTime.Parse(src.StartDate +""+ src.StartTime),然后验证EndDateTime与StartDateTime …
我尝试使用GreaterThen验证器,看起来它不支持客户端验证.是否有支持客户端验证的FluentValidation验证器列表?
这更像是一个理论问题.
我目前正在使用ComponentModel.DataAnnotations检查MVC 3验证,一切都在自动运行,特别是在客户端.
某种程度上会检查这些属性,并为验证生成javascript(或html5属性,如果使用不显眼的模式),它可以工作.
我的问题是什么生成客户端javascript以及如何访问和修改它?例如,我想稍微处理给定的dataannotation属性,或处理自定义属性(我发现我可以从ValidationAttribute派生它们,但可能由于某些原因我不想要).
有人可以向我解释一下真正发生了什么吗?(或者链接到好的解释也会很好,因为我只找到了实际使用数据注释的教程)
编辑:此外,从ValidationAttribute派生,客户端验证不会自动运行.为什么?
我遇到了一个问题,我遇到了日期时间字段的客户端验证问题.当我尝试提交时,它一直告诉我日期无效(2013年7月27日).但是,如果我将日期转换为美国格式(2013年7月27日).
我的观点模型如下,
[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? AuditDate { get; set; }
Run Code Online (Sandbox Code Playgroud)
的index.html
@Html.TextBoxFor(m => m.AuditDate)
Run Code Online (Sandbox Code Playgroud)
我已经更新了我的web.config
<globalization culture="en-AU" uiCulture="en-AU" />
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
谢谢