在 asp.net mvc 中使用数据注释验证唯一性

发布于 2024-09-26 12:05:19 字数 1975 浏览 0 评论 0原文

我对使用数据注释进行验证有各种疑问。我正在使用以下设置

asp.net mvc 2 实体框架4 数据注释

基本上,我试图让独特的验证工作,但我有点困惑。我的模型如下:

    public class Buyer
{
    public int Id { get; set; }

    [Required(ErrorMessage = "The email is required")]
    public string Email { get; set; }

    [Required(ErrorMessage= "The name is required")]
    public string Name { get; set; }
}

    public class Seller
{
    public int Id { get; set; }

    [Required(ErrorMessage = "The email is required")]
    public string Email { get; set; }

    [Required(ErrorMessage= "The name is required")]
    public string Name { get; set; }
}

我已经设置了一个唯一的字段属性,如下所示

public class UniqueFieldAttribute : ValidationAttribute
{
    public IUniqueValidator Validator { get; set; }
    public int Id { get; set; }

    public override bool IsValid(object value)
    {
        if (value == null)
        {
            return true;
        }

        return Validator.IsValid(Convert.ToString(value), Id);
    }            
}

,然后我创建了一个实现 IUniqueValidator 接口的验证器:

public class BuyerUniqueEmailValidator : IUniqueValidator
{
    public bool IsValid(string value, int id)
    {
        TheDb db = new TheDb();


        var existing = from Buyer b in db.Buyers
                       where b.Email.ToLower() == value.ToLower()
                       select b;

        foreach (Buyer b in existing)
        {
            if (b.Id != id)
            {
                return false;
            }
        }

        return true;
    }
}

这个想法就在那里!然而,在执行时我遇到了问题。当我添加此

[UniqueField(Validator=new BuyerUniqueEmailValidator(), Id=this.Id ErrorMessage= "This email is in use")]

项目时,该项目将无法编译。

基本上,我想知道是否可以将一个类传递给validationAttribute来执行验证?另外,如何传递 id。

另外,是否有办法创建一个通用的唯一字段生成器,该生成器适用于我所有具有电子邮件字段的模型,或者我是否必须拥有 BuyerEmailValidator、SellerEmailValidator 等。我似乎无法让 T 正常工作。

目前只担心服务器端。

谢谢

I have various questions about validation using data annotations. I am using the following setup

asp.net mvc 2
entity framework 4
data annotations

Basically, I'm trying to get unique validation working and i'm a little confused. My models are as follows:

    public class Buyer
{
    public int Id { get; set; }

    [Required(ErrorMessage = "The email is required")]
    public string Email { get; set; }

    [Required(ErrorMessage= "The name is required")]
    public string Name { get; set; }
}

    public class Seller
{
    public int Id { get; set; }

    [Required(ErrorMessage = "The email is required")]
    public string Email { get; set; }

    [Required(ErrorMessage= "The name is required")]
    public string Name { get; set; }
}

I have set up a unique field attribute as follows

public class UniqueFieldAttribute : ValidationAttribute
{
    public IUniqueValidator Validator { get; set; }
    public int Id { get; set; }

    public override bool IsValid(object value)
    {
        if (value == null)
        {
            return true;
        }

        return Validator.IsValid(Convert.ToString(value), Id);
    }            
}

I then created a validator that implements the IUniqueValidator interface:

public class BuyerUniqueEmailValidator : IUniqueValidator
{
    public bool IsValid(string value, int id)
    {
        TheDb db = new TheDb();


        var existing = from Buyer b in db.Buyers
                       where b.Email.ToLower() == value.ToLower()
                       select b;

        foreach (Buyer b in existing)
        {
            if (b.Id != id)
            {
                return false;
            }
        }

        return true;
    }
}

The idea is there! However, on execution I am having problems. When I add this

[UniqueField(Validator=new BuyerUniqueEmailValidator(), Id=this.Id ErrorMessage= "This email is in use")]

the project won't compile.

Basically, what I want to know is if it is possible to pass a class to the validationAttribute to perform the validation? Also, how can i pass an id.

Additionally, is there anyway to create a generic unique field generator that would work for all my models that have an email field, or do I have to have a BuyerEmailValidator, a SellerEmailValidator etc, etc. I can't seem to get T working correctly.

Only worried about serverside at the moment.

Thanks

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

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

发布评论

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

评论(1

流年里的时光 2024-10-03 12:05:19

您的代码实现无法看到对其自己的事务不可见的任何记录,因此无法在多用户环境中可靠地工作。为什么不使用数据库约束?

Your code implementation of this can't see any records not visible to its own transaction, and, hence, can't work reliably in a multi-user environment. Why not use a DB constraint?

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