0

次の ValidationAttribute クラスがあります

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class DateValidationAttribute : ValidationAttribute
{
    public DateValidationAttribute(string leftDateProperty, CompareOperator compareOperator, string rightDateProperty, string errorMessage)
            : base(errorMessage)
    {
        LeftDateProperty = leftDateProperty;
        Operator = compareOperator;
        RightDateProperty = rightDateProperty;
    }
    ...
    ...
}

コンストラクターで 2 つの日付プロパティ名と演算子を使用します。

検証メソッドでは、ステートメント LeftDate Operator RightDate の結果が返されます。

public override bool IsValid(object value)
{
    DateTime leftDate;
    DateTime rightDate;

    // Get all properties on the view model
    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);

    DateTime rightDate = (DateTime)properties.Find(RightDateProperty, true).GetValue(value);
    DateTime leftDate = (DateTime)properties.Find(LeftDateProperty, true).GetValue(value);

    // Perform rule check
    switch (Operator)
    {
        case CompareOperator.Equal:
            return leftDate.Equals(rightDate);
        case CompareOperator.Greater:                    
            return leftDate > rightDate;
        case CompareOperator.Lesser:                    
            return leftDate < rightDate;
        case CompareOperator.GreaterOrEqual:                    
            return leftDate >= rightDate;
        case CompareOperator.LesserOrEqual:                    
            return leftDate <= rightDate;
        default:
            return false;
    }
}

これは AttriuteTargets.Class 属性であるため、検証が失敗する原因となっているプロパティをフレームワークが認識できないことはわかっています。しかし、失敗しているのは Left Date プロパティであることを知っているため、モデル状態のエラーの ID をこのプロパティに設定したいと考えています。この理由は、失敗したフィールドをフォームでマークしたいからです。

質問: ModelState のエラー コレクションに追加されたエラー項目を変更して、その ID がフォームの特定のフィールドに対応するようにするにはどうすればよいですか?

4

1 に答える 1

-1

IDataErrorInfo を使用してこれを行うより良い方法を見つけました

これが私がやっている方法です。質問の例ほど一般的ではありません。このソリューションでは、各チェックをコーディングする必要があります。しかし今では、検証は、失敗した入力要素の JavaScript と強調表示までうまく機能します。

public class TestModel: IDataErrorInfo
{

    public TestModel()
    {
    }

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

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

    #region IDataErrorInfo Members

    public string Error
    {
        get
        {
            return string.Empty;
        }
    }

    public string this[string columnName]
    {
        get
        {
            switch (columnName)
            {
                case "StartDate":
                    {
                        if (StartDate < DateTime.Today)
                        {
                            return "Start date must not be a date in the past";
                        }
                        break;
                    }
                case "EndDate":
                    {
                        if (EndDate < StartDate)
                        {
                            return "End date must not be a date before start date";
                        }
                        break;
                    }   
                default:
                    return string.Empty;
            }
            return string.Empty;
        }
    }
}
于 2010-07-02T08:21:32.160 に答える