views:

35

answers:

1

Hi, 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

A: 

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?

Craig Stuntz
I will add a db constraint in production. However, this is more of a philosophical question. That the emails should be unique is a business rule and therefore it should be implemented and tested within my domain model.
I understand what you want, but I'm also presuming you want it to work reliably. You *can't* do this at the model level and have it work 100% of the time.
Craig Stuntz