えムナウ Blog

えムナウ の なすがまま

目次

Blog 利用状況

ニュース


follow mnow at http://twitter.com


えムナウのプログラミングのページ

INETAJ

書庫

日記カテゴリ

ギャラリ

System.ComponentModel.DataAnnotations 入力検証に革命が

System.ComponentModel.DataAnnotations は入力検証に革命をもたらす。

こんな属性をプロパティにつける。
[Display(Name="名前", Description="10文字以内で入力してください")]  
[Required(ErrorMessage="名前を入力してください")]  
[StringLength(10, ErrorMessage="名前は10文字以内で入力してください")]

プロパティのsetの PropertyChanged イベント発生させる前にでもこんな文を追加する。
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "FullName" });

WPF でも Silverlight でも Control の Binding に ValidatesOnExceptions=True, NotifyOnValidationError=True をつけてやるとエラーを表示する。

WPF の場合は Validation.ErrorTemplate とか ToolTip とかエラー表示の Visual を考えないといけないが (デフォルトは周囲が赤くなるだけ)。
Displayもサポートされていないが・・・そのうちサポートしてくるんでしょう。

属性で入力検証出来るなんて革命的すぎる。

http://blogs.wankuma.com/kazuki/archive/2009/07/21/178723.aspx

一通りのチェックはできそうだ。

CustomValidationAttribute
RangeAttribute
RegularExpressionAttribute
RequiredAttribute
StringLengthAttribute

投稿日時 : 2009年7月27日 15:07

コメントを追加

# re: System.ComponentModel.DataAnnotations 入力検証に革命が 2009/09/12 11:10 a_山田

こんにちは。
WPFでの利用の場合は、.net 4 になりますよね?

# re: System.ComponentModel.DataAnnotations 入力検証に革命が 2009/09/17 9:54 えムナウ

そうですね。

# re: System.ComponentModel.DataAnnotations 入力検証に革命が 2009/09/17 9:58 えムナウ

一応3.5でも入っているようですが・・・
http://msdn.microsoft.com/ja-jp/library/cc488527.aspx

# re: System.ComponentModel.DataAnnotations 入力検証に革命が 2009/09/20 13:14 えムナウ

Validator.ValidateProperty は3.5にはないみたいです。

# Exceptionally user pleasant site. Immense info available on few clicks on. 2021/11/03 15:54 Exceptionally user pleasant site. Immense info ava

Exceptionally user pleasant site. Immense info available on few clicks
on.

# Simply had to say I am thrilled I happened on the web site. 2021/11/04 21:54 Simply had to say I am thrilled I happened on the

Simply had to say I am thrilled I happened on the web site.

タイトル
名前
URL
コメント