ASP.NET MVC 2 RC - 使用数据注释的客户端验证因多个属性而失败

发布于 2024-08-17 06:28:52 字数 2218 浏览 3 评论 0原文

在模型上使用数据注释时,我在为客户端验证发出 JavaScript 时遇到问题。下面是一个模型示例,它可以很好地处理两个必填字段,后跟发出的 JavaScript:

public class LoginUserViewModel
    {
        [Required(ErrorMessage = "Email required")]
        public string EmailAddress { get; set; }

        [Required(ErrorMessage="Password required")]
        public string Password { get; set; }
    }
}

//<![CDATA[
if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; }
window.mvcClientValidationMetadata.push({"Fields":[{"FieldName":"EmailAddress","ReplaceValidationMessageContents":true,"ValidationMessageId":"form0_EmailAddress_validationMessage","ValidationRules":[{"ErrorMessage":"Email required","ValidationParameters":{},"ValidationType":"required"}]},{"FieldName":"Password","ReplaceValidationMessageContents":true,"ValidationMessageId":"form0_Password_validationMessage","ValidationRules":[{"ErrorMessage":"Password required","ValidationParameters":{},"ValidationType":"required"}]}],"FormId":"form0","ReplaceValidationSummary":false});
//]]> 

一旦我在 EmailAddress 字段上放置另一个属性(正则表达式属性),JavaScript 就不再发出任何规则。请注意,下面的 JavaScript 中只有一个空数组,其中应该包含规则。这是更改和脚本。有什么想法吗?

public class LoginUserViewModel
    {
        [Required(ErrorMessage = "Email required")]
        [RegularExpression(@"^[a-z0-9]+([-+\.]*[a-z0-9]+)*@[a-z0-9]+([-\.][a-z0-9]+)*{2,4}$", ErrorMessage = "Invalid email format")]
        public string EmailAddress { get; set; }

        [Required(ErrorMessage="Password required")]
        public string Password { get; set; }
    }

//<![CDATA[
if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; }
window.mvcClientValidationMetadata.push({"Fields":[{"FieldName":"EmailAddress","ReplaceValidationMessageContents":true,"ValidationMessageId":"form0_EmailAddress_validationMessage","ValidationRules":[]},{"FieldName":"Password","ReplaceValidationMessageContents":true,"ValidationMessageId":"form0_Password_validationMessage","ValidationRules":[{"ErrorMessage":"Password is required.","ValidationParameters":{},"ValidationType":"required"}]}],"FormId":"form0","ReplaceValidationSummary":false});
//]]> 

有什么想法导致添加新属性时规则消失吗?

谢谢!

I'm having trouble with the JavaScript being emitted for client side validation when using Data Annotations on a model. Here's a sample of a model that works just fine with two required fields followed by the JavaScript that's emitted:

public class LoginUserViewModel
    {
        [Required(ErrorMessage = "Email required")]
        public string EmailAddress { get; set; }

        [Required(ErrorMessage="Password required")]
        public string Password { get; set; }
    }
}

//<![CDATA[
if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; }
window.mvcClientValidationMetadata.push({"Fields":[{"FieldName":"EmailAddress","ReplaceValidationMessageContents":true,"ValidationMessageId":"form0_EmailAddress_validationMessage","ValidationRules":[{"ErrorMessage":"Email required","ValidationParameters":{},"ValidationType":"required"}]},{"FieldName":"Password","ReplaceValidationMessageContents":true,"ValidationMessageId":"form0_Password_validationMessage","ValidationRules":[{"ErrorMessage":"Password required","ValidationParameters":{},"ValidationType":"required"}]}],"FormId":"form0","ReplaceValidationSummary":false});
//]]> 

as soon as I put another attribute on the EmailAddress field, a regular expression attribute, the JavaScript no longer has any rules emitted. Notice in the JavaScript below there is just an empty array where the rules should be. Here's the change and the script. Any ideas?

public class LoginUserViewModel
    {
        [Required(ErrorMessage = "Email required")]
        [RegularExpression(@"^[a-z0-9]+([-+\.]*[a-z0-9]+)*@[a-z0-9]+([-\.][a-z0-9]+)*{2,4}$", ErrorMessage = "Invalid email format")]
        public string EmailAddress { get; set; }

        [Required(ErrorMessage="Password required")]
        public string Password { get; set; }
    }

//<![CDATA[
if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; }
window.mvcClientValidationMetadata.push({"Fields":[{"FieldName":"EmailAddress","ReplaceValidationMessageContents":true,"ValidationMessageId":"form0_EmailAddress_validationMessage","ValidationRules":[]},{"FieldName":"Password","ReplaceValidationMessageContents":true,"ValidationMessageId":"form0_Password_validationMessage","ValidationRules":[{"ErrorMessage":"Password is required.","ValidationParameters":{},"ValidationType":"required"}]}],"FormId":"form0","ReplaceValidationSummary":false});
//]]> 

Any ideas what's causing the rules to disappear when the new attribute is added?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

生活了然无味 2024-08-24 06:28:52

经过进一步调查,问题是你的正则表达式被破坏了,因为你连续指定了两个量词:

parsing "[a-z0-9]+([-+\.]*[a-z0-9]+)*@[a-z0-9]+([-\.][a-z0-9]+)*{2,4}"
- Nested quantifier {.

所有规则似乎消失的原因是因为 CLR 吞下了属性构造函数的异常,并只是告诉你“对不起” ,这里没有属性。”

After further investigation, the problem is that your regular expression is broken, because you've specified two quantifiers back-to-back:

parsing "[a-z0-9]+([-+\.]*[a-z0-9]+)*@[a-z0-9]+([-\.][a-z0-9]+)*{2,4}"
- Nested quantifier {.

The reason all the rules seem to disappear is because the CLR swallows exceptions from attribute constructors and just tells you "sorry, there are no attributes here."

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