0

別のフィールドに依存するデータ型の検証に問題があります。ここで見つけた例のほとんどは、別のフィールドの値に基づいてフィールドを必須または非必須にするためのものです ( isMaidenNameの場合にのみ必須になります)。IsMarriedtrue

私のモデル

public class AttributeValuesModel
{
    public IList<AttributeModel> Values {get; set;}
}

public class AttributeModel
{
    [Required]
    public string AttributeName {get; set;}

    [Required]
    public string AttributeValue {get; set;}

    [Required]
    public int DataTypeId {get; set;}
}

私がやりたいことは、DataTypeId の値に基づいて AttributeValue のユーザー入力を検証することです。わかりやすくするためにDataTypeId、ビューをユーザーに表示する前に、 の値がわかっています。

    //Possible values for DataTypeId are 
    //1 for decimal
    //2 for dates
    //3 for integer

これは可能ですか?

4

2 に答える 2

1

FoolProofASP.NET MVCの検証拡張機能をご覧ください。などの条件付き検証を実行するために使用できる検証属性が含まれてい[RequiredIf]ます。

さらに強力な検証ライブラリ (および私が使用し、推奨するもの) はFluentMVC. 検証データの注釈とは対照的に、このライブラリを使用すると、宣言型ではなく命令型の検証を実行できます。これにより、任意の複雑さのルールと依存プロパティ間のルールを表現できます。

于 2013-01-23T16:19:05.437 に答える
0

It's not so hard to roll your own validation attribute. I have implemented one some time ago. It checks whether value of other property is smaller than property that is decorated with this attribiute:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited=true)]
public class SmallerThanAttribute : ValidationAttribute
{
    public SmallerThanAttribute(string otherPropertyName)
    {
        this.OtherPropertyName = otherPropertyName;
    }

    public string OtherPropertyName { get; set; }
    public string OtherPropertyDisplayName { get; set; }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        return IsValid(OtherPropertyName, value, validationContext);
    }

    private ValidationResult IsValid(string otherProperty, object value, ValidationContext validationContext)
    {
        PropertyInfo otherPropertyInfo = validationContext.ObjectType.GetProperty(otherProperty);

        if (otherPropertyInfo == null)
        {
            throw new Exception("Could not find property: " + otherProperty);
        }

        var displayAttribute = otherPropertyInfo.GetCustomAttributes(typeof(DisplayAttribute), true).FirstOrDefault() as DisplayAttribute;

        if (displayAttribute != null && OtherPropertyDisplayName == null)
        {
            OtherPropertyDisplayName = displayAttribute.GetName();
        }

        object otherPropertyValue = otherPropertyInfo.GetValue(validationContext.ObjectInstance, null);

        var smaller = (IComparable) value;
        var bigger = (IComparable) otherPropertyValue;

        if (smaller == null || bigger == null)
        {
            return null;
        }

        if (smaller.CompareTo(bigger) > 0)
        {
            return new ValidationResult(string.Format(ValidatorResource.SmallerThan, validationContext.DisplayName, OtherPropertyDisplayName));
        }

        return null;
    }
}

There is one gotcha. Error message format is defined in resource class property (ValidatorResource.SmallerThan), so it's not pluggable -- I didn't need this. However, I think it still could be a good starting point for you.

于 2013-01-23T19:09:29.287 に答える