开发者

MVC2 model validation mutually exclusive required annotation

Does anybody know of a good开发者_运维百科 algorithm to mutually exclusively check two properties using a ModelValidator?

Something like:

    [EitherPropertyRequired("BuildingNumber","BuildingName"]
    public class Address{
       public int BuildingNumber { get; set; }
       public string BuildingName { get; set; }
   }


I ended up creating an attribute and manually checking it with a custom ModelValidator. This custom model validator is checked using an AssociatedValidatorProvider which is registered in Application_Start().

protected void Application_Start()
{
    ModelValidatorProviders.Providers.Add(new ZipValidationProvider());
}

public class ZipValidationProvider:AssociatedValidatorProvider
{
    protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
    {
        foreach (var attribute in attributes.OfType<EitherPropertyRequiredAttribute>())
        {
            yield return new EitherPropertyRequiredValidator(metadata,
                context, attribute.FirstProperty, attribute.SecondProperty, attribute.Message);
        }
    }
}

[AttributeUsage(AttributeTargets.Class)]
public class EitherPropertyRequiredAttribute : Attribute
{
    public readonly string FirstProperty;
    public readonly string SecondProperty;
    public readonly string Message;

    public EitherPropertyRequiredAttribute(string firstProperty, string secondProperty, 
        string message)
    {
        FirstProperty = firstProperty;
        SecondProperty = secondProperty;
        Message = message;
    }
}

public class EitherPropertyRequiredValidator:ModelValidator
{
    private readonly string firstProperty;
    private readonly string secondProperty;
    private readonly string message;

    public EitherPropertyRequiredValidator(ModelMetadata metadata,
                                    ControllerContext context,
        string firstProperty, 
        string secondProperty,
        string message)
        :base(metadata,context)
    {
        this.firstProperty = firstProperty;
        this.secondProperty = secondProperty;
        this.message = message;
    }

    private PropertyInfo GetPropertyInfoRecursive(Type type, string property)
    {
        var prop = type.GetProperty(property);
        if (prop != null) return prop;

        foreach (var p in type.GetProperties())
        {
            if (p.PropertyType.Assembly == typeof (object).Assembly)
                continue;

            return GetPropertyInfoRecursive(p.PropertyType, property);
        }

        return null;
    }

    private object GetPropertyValueRecursive(object obj, PropertyInfo propertyInfo)
    {
        Type objectType = obj.GetType();

       if(objectType.GetProperty(propertyInfo.Name) != null)
            return propertyInfo.GetValue(obj, null);

        foreach (var p in objectType.GetProperties())
        {
            if (p.PropertyType.Assembly == typeof(object).Assembly)
                continue;

            var o = p.GetValue(obj,null);
            return GetPropertyValueRecursive(o, propertyInfo);
        }

        return null;
    }

    public override IEnumerable<ModelValidationResult> Validate(object container)
    {
        if (Metadata.Model == null)
            yield break;

        var firstPropertyInfo = GetPropertyInfoRecursive(Metadata.Model.GetType(),firstProperty);
        if(firstPropertyInfo == null)
            throw new InvalidOperationException("Unknown property:" + firstProperty);

        var secondPropertyInfo = GetPropertyInfoRecursive(Metadata.Model.GetType(),secondProperty);
        if(secondPropertyInfo == null)
            throw new InvalidOperationException("Unknown property:" + secondProperty);

        var firstPropertyValue = GetPropertyValueRecursive(Metadata.Model, firstPropertyInfo);
        var secondPropertyValue = GetPropertyValueRecursive(Metadata.Model, secondPropertyInfo);

        bool firstPropertyIsEmpty = firstPropertyValue == null ||
                                    firstPropertyValue.ToString().Length == 0;

        bool secondPropertyIsEmpty = secondPropertyValue == null ||
                                     secondPropertyValue.ToString().Length == 0;

        if (firstPropertyIsEmpty && secondPropertyIsEmpty)
        {
                yield return new ModelValidationResult
                {
                    MemberName = firstProperty,
                    Message = message
                };
        }
    }
}


[AttributeUsage(AttributeTargets.Class)]
public class EitherPropertyRequiredAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        // value will be the model

        Address address = (Address)value;

        // TODO: Check the properties of address here and return true or false

        return true;
    }
}

You could make this more generic by avoiding it casting to Address and using attribute properties and reflection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜