ASP.NET MVC2 正则表达式奇怪的验证问题 (DataAnnotations)

发布于 2024-09-06 19:15:16 字数 1154 浏览 8 评论 0原文

我已经通过服务层连接了验证,我的生日属性如下所示。

    <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若相惜即相离 2024-09-13 19:15:16

假设用户在文本字段中输入 06/27/2010 并提交表单。

  1. 默认模型绑定器运行并尝试将用户输入的内容绑定到您的模型。 BirthDate 属性的类型为 DateTime 并且如您所知,此类型包含小时部分。现在 BirthDate = 6/27/2010 12:00:00 AM

  2. 接下来开始验证。 BirthDate 属性用 RegularExpression 属性,因此 <使用字符串值 6/27/2010 12:00:00 AM 调用 code>IsValid 方法,该方法根据您的模式失败。

  3. 向用户显示相应的错误消息。

所以这个正则表达式属性的使用是有问题的。 RegularExpressionAttribute 不过,可以很好地处理字符串属性。

如果用户输入无效日期,模型绑定程序将在验证发生之前提出抗议。它还会检查正则表达式不会检查的无效日期和月份。

Suppose the user enters 06/27/2010 in the text field and submits the form.

  1. The default model binder runs and tries to bind what the user entered to your model. The BirthDate property is of type DateTime and as you know this type contains an hour part. So now BirthDate = 6/27/2010 12:00:00 AM

  2. Validation kicks in next. The BirthDate property is decorated with the RegularExpression attribute so the IsValid method is called with the string value of 6/27/2010 12:00:00 AM which fails against your pattern.

  3. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文