5

サーバーとクライアントの間で共有される ValidationAttribute を作成しました。データ ヘルパー クラス内で参照されたときにクライアントに適切に生成される検証属性を取得するには、その構築方法を非常に具体的にする必要がありました。

私が抱えている問題は、何らかの理由で、カスタム検証属性クラスから ValidationResult を返すときに、クライアント UI の他の検証属性と同じように処理されないことです。エラーを表示する代わりに、何もしません。ただし、オブジェクトは適切に検証されますが、失敗した検証結果は表示されません。

以下は、私のカスタム検証クラスの 1 つのコードです。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace Project.Web.DataLayer.ValidationAttributes
{
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
    public class DisallowedChars : ValidationAttribute
    {
        public string DisallowedCharacters
        {
            get
            {
                return new string(this.disallowedCharacters);
            }

            set
            {
                this.disallowedCharacters = (!this.CaseSensitive ?     value.ToLower().ToCharArray() : value.ToCharArray());
            }
        }

        private char[] disallowedCharacters = null;

        private bool caseSensitive;

        public bool CaseSensitive
        {
            get
            {
                return this.caseSensitive;
            }

            set
            {
                this.caseSensitive = value;
            }
        }

        protected override ValidationResult IsValid(object value, ValidationContext    validationContext)
        {
            if (value != null && this.disallowedCharacters.Count() > 0)
            {
                string Value = value.ToString();

                foreach(char val in this.disallowedCharacters)
                {
                    if ((!this.CaseSensitive && Value.ToLower().Contains(val)) ||     Value.Contains(val))
                    {
                        return new ValidationResult(string.Format(this.ErrorMessage != null ? this.ErrorMessage : "'{0}' is not allowed an allowed character.", val.ToString()));
                    }
                }
            }

            return ValidationResult.Success;
        }
    }
}

これは、サーバーとクライアントの両方でプロパティの上で使用する方法です。

[DisallowedChars(DisallowedCharacters = "=")]

そして、バインディングをセットアップするいくつかの異なる方法を試しました。

{Binding Value, NotifyOnValidationError=True}

としても

{Binding Value, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True, ValidatesOnNotifyDataErrors=True}

これらのいずれも、バインドされているフォームがエントリを検証するようには見えません。TextBoxes、XamGrids にバインドされた値でこの属性を使用しようとしましたが、どちらも適切に検証されませんでした。

この問題は、サーバー側で ValidationResult を使用しようとしているときにのみ発生するようです。ビューモデルの値で検証結果を使用すると、適切に検証されます。ただし、生成されたコードからこれを適切に検証する方法を見つける必要があります。

どんな考えでも大歓迎です。

4

1 に答える 1

4

ValidationResult に関連付けられている MemberNames を指定する必要があります。ValidationResult のコンストラクターには、結果に関連付けられているプロパティを指定する追加のパラメーターがあります。プロパティを指定しない場合、結果はエンティティ レベルの検証エラーとして処理されます。

したがって、あなたの場合、プロパティの名前を ValidationResult のコンストラクターに渡すときに修正する必要があります。

protected override ValidationResult IsValid(object value, ValidationContext validationContext) {
 if (value != null && this.disallowedCharacters.Count() > 0) {
   string Value = value.ToString();

   foreach(char val in this.disallowedCharacters) {
     if ((!this.CaseSensitive && Value.ToLower().Contains(val)) || Value.Contains(val)) {
       //return new ValidationResult(string.Format(this.ErrorMessage != null ? this.ErrorMessage : "'{0}' is not allowed an allowed character.", val.ToString()));
       string errorMessage = string.Format(this.ErrorMessage != null ? this.ErrorMessage : "'{0}' is not allowed an allowed character.", val.ToString());
       return new ValidationResult(errorMessage, new string[] { validationContext.MemberName});
     }
   }
 }

 return ValidationResult.Success;
}

バインディングについては、他に何も指定する必要はありません。したがって、単純なバインディング

{Binding Value}

ValidatesOnNotifyDataErrorsが暗黙的に true に設定されているため、エラーを表示する必要があります。NotifyOnValidationErrorは、ValidationSummary などの他の要素に ValidationErrors を取り込みます。

Jeff Handly は、WCF Ria サービスと Silverlight の検証に関する非常に優れたブログ投稿を行っています。読むことをお勧めします。

于 2011-11-17T08:57:13.213 に答える