ASP.NET MVC2 正则表达式奇怪的验证问题 (DataAnnotations)
我已经通过服务层连接了验证,我的生日属性如下所示。
<DisplayName("birthdate")> _
<RegularExpression("^\d{2}\/\d{2}\/\d{4}$", ErrorMessage:="Birthdate is not in the correct format.")> _
<DisplayFormat(ApplyFormatInEditMode:=True, ConvertEmptyStringToNull:=True, DataFormatString:="{0:MM/dd/yyyy}")> _
Public Property BirthDate As DateTime
如果我输入类似 12/12/1990
的内容,客户端验证可以正常工作,但是当提交表单时,服务器端验证将失败,并且我被告知该条目无效。我使用 jQuery-UI Datepicker 输入日期,但是当我禁用 datepicker 时,问题仍然存在。
我在这里错过了什么吗?我认为客户端和服务器端是同一件事。
事后思考。
可能是因为我在 SQL Server 数据库中使用 datetime2(0)
吗?
我测试了上述理论,但没有成功...同样的问题。
编辑:
如果我删除
<RegularExpression("^\d{2}\/\d{2}\/\d{4}$", ErrorMessage:="Birthdate is not in the correct format.")>
则表单提交。这显然与正则表达式有关。
编辑:
以下代码也不起作用,但我想知道是否有办法修改 modelState 以在传递给 IsValid 方法之前正确设置日期?
Dim birthdate As String = user.BirthDate
ModelState.Item("BirthDate") = DateTime.Parse(birthdate)
I've got my validation wired up through my Service layer, and my Birthdate property looks like this.
<DisplayName("birthdate")> _
<RegularExpression("^\d{2}\/\d{2}\/\d{4}$", ErrorMessage:="Birthdate is not in the correct format.")> _
<DisplayFormat(ApplyFormatInEditMode:=True, ConvertEmptyStringToNull:=True, DataFormatString:="{0:MM/dd/yyyy}")> _
Public Property BirthDate As DateTime
The client side validation works properly if I input something like 12/12/1990
but when the form is submitted, the server side validation trips and I'm told the entry is invalid. I'm using the jQuery-UI Datepicker to input the date, however when I disable the datepicker, the problem persists.
Am I missing something here? I thought the client side and server side would be the same thing.
Afterthought.
Could it be because I'm using datetime2(0)
in my SQL Server database?
I tested the above theory to no avail... same problem.
EDIT:
If I remove
<RegularExpression("^\d{2}\/\d{2}\/\d{4}$", ErrorMessage:="Birthdate is not in the correct format.")>
Then the form submits. It's obviously something to do with the Regex.
EDIT:
The following code doesn't work either, but I'm wondering if there is a way to modify the modelState to properly set the date before it's passed to the IsValid method?
Dim birthdate As String = user.BirthDate
ModelState.Item("BirthDate") = DateTime.Parse(birthdate)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设用户在文本字段中输入
06/27/2010
并提交表单。默认模型绑定器运行并尝试将用户输入的内容绑定到您的模型。
BirthDate
属性的类型为DateTime
并且如您所知,此类型包含小时部分。现在BirthDate = 6/27/2010 12:00:00 AM
接下来开始验证。
BirthDate
属性用RegularExpression
属性,因此 <使用字符串值6/27/2010 12:00:00 AM
调用 code>IsValid 方法,该方法根据您的模式失败。向用户显示相应的错误消息。
所以这个正则表达式属性的使用是有问题的。 RegularExpressionAttribute 不过,可以很好地处理字符串属性。
如果用户输入无效日期,模型绑定程序将在验证发生之前提出抗议。它还会检查正则表达式不会检查的无效日期和月份。
Suppose the user enters
06/27/2010
in the text field and submits the form.The default model binder runs and tries to bind what the user entered to your model. The
BirthDate
property is of typeDateTime
and as you know this type contains an hour part. So nowBirthDate = 6/27/2010 12:00:00 AM
Validation kicks in next. The
BirthDate
property is decorated with theRegularExpression
attribute so theIsValid
method is called with the string value of6/27/2010 12:00:00 AM
which fails against your pattern.The corresponding error message is shown to the user.
So the usage of this regular expression attribute is questionable. RegularExpressionAttribute works fine with string properties though.
If the user enters an invalid date the model binder will protest anyway much before the validation happens. It will also check for invalid days and months which your regular expression doesn't.