基于其他字段的验证?

发布于 2024-09-07 15:59:45 字数 189 浏览 1 评论 0原文

在 ASP.NET MVC 2 中,我有一个包含一系列字段的 Linq to sql 类。现在,当另一个字段具有特定(枚举)值时,其中一个字段是必需的。

到目前为止,我编写了一个自定义验证属性,它可以将枚举作为属性,但我不能说,例如: EnumValue = this.OtherField

我应该怎么做?

In ASP.NET MVC 2, I have a Linq to sql class that contains a series of fields. Now I one of the fields is required when another field has a certain (enum) value.

I've come so far that I wrote a custom validation attribute, which can take an enum as an attribute, but I can't say, for example: EnumValue = this.OtherField

How should I do that?

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

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

发布评论

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

评论(1

冬天旳寂寞 2024-09-14 15:59:45

MVC2 附带了一个示例“PropertiesMustMatchAttribute”,它展示了如何让 DataAnnotations 为您工作,并且它应该在 .NET 3.5 和 .NET 4.0 中工作。该示例代码如下所示:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] 
public sealed class PropertiesMustMatchAttribute : ValidationAttribute 
{ 
    private const string _defaultErrorMessage = "'{0}' and '{1}' do not match."; 

    private readonly object _typeId = new object(); 

    public PropertiesMustMatchAttribute(string originalProperty, string confirmProperty) 
        : base(_defaultErrorMessage) 
    { 
        OriginalProperty = originalProperty; 
        ConfirmProperty = confirmProperty; 
    } 

    public string ConfirmProperty 
    { 
        get; 
        private set; 
    } 

    public string OriginalProperty 
    { 
        get; 
        private set; 
    } 

    public override object TypeId 
    { 
        get 
        { 
            return _typeId; 
        } 
    } 

    public override string FormatErrorMessage(string name) 
    { 
        return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString, 
            OriginalProperty, ConfirmProperty); 
    } 

    public override bool IsValid(object value) 
    { 
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value); 
        // ignore case for the following
        object originalValue = properties.Find(OriginalProperty, true).GetValue(value); 
        object confirmValue = properties.Find(ConfirmProperty, true).GetValue(value); 
        return Object.Equals(originalValue, confirmValue); 
    } 
} 

当您使用该属性时,不是将其放在模型类的属性上,而是将其放在类本身上:

[PropertiesMustMatch("NewPassword", "ConfirmPassword", ErrorMessage = "The new password and confirmation password do not match.")] 
public class ChangePasswordModel 
{ 
    public string NewPassword { get; set; } 
    public string ConfirmPassword { get; set; } 
} 

当在自定义属性上调用“IsValid”时,将传递整个模型实例这样您就可以通过这种方式获取依赖属性值。您可以轻松地遵循此模式来创建更通用的比较属性。

MVC2 comes with a sample "PropertiesMustMatchAttribute" that shows how to get DataAnnotations to work for you and it should work in both .NET 3.5 and .NET 4.0. That sample code looks like this:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] 
public sealed class PropertiesMustMatchAttribute : ValidationAttribute 
{ 
    private const string _defaultErrorMessage = "'{0}' and '{1}' do not match."; 

    private readonly object _typeId = new object(); 

    public PropertiesMustMatchAttribute(string originalProperty, string confirmProperty) 
        : base(_defaultErrorMessage) 
    { 
        OriginalProperty = originalProperty; 
        ConfirmProperty = confirmProperty; 
    } 

    public string ConfirmProperty 
    { 
        get; 
        private set; 
    } 

    public string OriginalProperty 
    { 
        get; 
        private set; 
    } 

    public override object TypeId 
    { 
        get 
        { 
            return _typeId; 
        } 
    } 

    public override string FormatErrorMessage(string name) 
    { 
        return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString, 
            OriginalProperty, ConfirmProperty); 
    } 

    public override bool IsValid(object value) 
    { 
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value); 
        // ignore case for the following
        object originalValue = properties.Find(OriginalProperty, true).GetValue(value); 
        object confirmValue = properties.Find(ConfirmProperty, true).GetValue(value); 
        return Object.Equals(originalValue, confirmValue); 
    } 
} 

When you use that attribute, rather than put it on a property of your model class, you put it on the class itself:

[PropertiesMustMatch("NewPassword", "ConfirmPassword", ErrorMessage = "The new password and confirmation password do not match.")] 
public class ChangePasswordModel 
{ 
    public string NewPassword { get; set; } 
    public string ConfirmPassword { get; set; } 
} 

When "IsValid" gets called on your custom attribute, the whole model instance is passed to it so you can get the dependent property values that way. You could easily follow this pattern to create even a more general comparison attribute.

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