views:

467

answers:

1

Hello

I'm having problems with getting my custom dataannotations to work, I'm trying to add a validation-attribute that validates that the UsergroupName for a Customer (CustomerID) is unique.

[MetadataType(typeof(UsergroupMetaData))]
public partial class Usergroup { }

public class UsergroupMetaData
{
    [Required()]
    public object CustomerID { get; set; }

    [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "UsergroupNameRequired")]
    public object UsergroupName { get; set; }

    [UniqueUsergroupName(????)]
    // what to put here?
}



public class UniqueUsergroupName : ValidationAttribute
{
    UsergroupRepository _rep = new UsergroupRepository();

    public override bool IsValid(object value, int customerID)
    {
        var x = _rep.GetUsergroups().ByUsergroupName(value).ByCustomerID(customerID);

        // what to put here?

        return false;
    }
}

the IsValid should return false if the "count >0".

How do I fix this one so it works. GetUsergroups() returns IQueryable.

EDIT:

[MetadataType(typeof(UsergroupMetaData))]
public partial class Usergroup { }

public class UsergroupMetaData
{
    public object CustomerID { get; set; }

    [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "UsergroupNameRequired")]
    [UniqueUsergroupName(ErrorMessageResourceType= typeof(Resources), ErrorMessageResourceName = "UsergroupNameExists")]
    public object UsergroupName { get; set; }


}


public class UniqueUsergroupName : ValidationAttribute
{

    UsergroupRepository _rep = new UsergroupRepository();

    public override bool IsValid(object value, int customerID)
    {

        int usergroups = _rep.GetUsergroups().ByCustomerID(customerID).ByUsergroupName(value.ToString()).Count();

        return usergroups >0;
    }
}

How can I pass the current CustomerID as a parameter?

/M

+2  A: 

You can apply this approach
http://byatool.com/mvc/custom-data-annotations-with-mvc-how-to-check-multiple-properties-at-one-time/
To include the ID property in your search, so that you check for all "other" UsergroupMetaData that have the same UsergroupName.

Check it and tel me if you have trouble applying it to your scenario.

Edit: More explanation

My understanding is that you need to check whether there are other UsergroupMetaData objects with the same UsergroupName.

Let's assume we'll make the validator become on your entire class not the property:

[UniqueUsergroupName]
public class UsergroupMetaData

No parameters are needed. Let's see how the UniqueUsergroupName Validate() method will look like:

public override Boolean IsValid(Object value)
{
  var usergroupName = value != null ? value.ToString() : null;
  //We don't validate empty fields, the Required validator does that
  if(string.IsNullOrEmpty(usergroupName)) return true;

  Type objectType = value.GetType();
  //Get the property info for the object passed in.  This is the class the attribute is
  //  attached to
  //I would suggest caching this part... at least the PropertyInfo[]
  PropertyInfo[] neededProperties =
    objectType.GetProperties();

  var customerIdProperty = neededProperties
    .Where(propertyInfo => propertyInfo.Name == "CustomerID")
    .First();
  var customerId = (int?) customerIdProperty.GetValue(value, null);

  var usergroupNameProperty = neededProperties
    .Where(propertyInfo => propertyInfo.Name == "UsergroupName")
    .First();
  var usergroupName = (string) customerIdProperty.GetValue(value, null);

  // Now I don't userstand why the blog post author did all this reflection stuff to 
  //   get the values of the properties. I don't know why he just didn't d something like:
  // var usergroup = (Usergroup) value;
  // var customerId = usergroup.CustomerId;
  // var usergroupName = usergroup.UsergroupName;
  //
  //I think reflection was not needed here. Try both ways anyway.
  // The next lines should not be different regardless of whether you used reflection.
  // 

  //We don't validate empty fields, the Required validator does that
  if(string.IsNullOrEmpty(usergroupName)) return true;

  //Now you have the customerId and usergroupName. Use them to validate.
  //If I'm using LINQ (for explanation only) it'd be something like:
  // Assuming _rep.GetUsergroups() returns IQueryable (only for explanation):
  int numberOfOtherUsergroupsWithSameName = 
        _rep.GetUsergroups()
              .Where(g => g.UsergroupName == usergroupName && g.CustomerId != customerId)
              .Count();
  return numberOfOtherUsergroupsWithSameName == 0;
}
Mohamed Meligy
I dont see how I should apply that in my scenario. Since it uses the database and all. Especially if CustomerID would be nullable and not required.
molgan
I added some written-on-the-fly code to explain how I think the idea may be used in your situation. Check it out and tell me if that works for you. The main catch as I see in the post is making the validation attribute work for the entire class not just a certain property. Tell me how it goes.
Mohamed Meligy