{"id":1290,"date":"2013-09-10T11:01:41","date_gmt":"2013-09-10T10:01:41","guid":{"rendered":"http:\/\/www.minddriven.de\/?p=1290"},"modified":"2013-09-12T13:20:38","modified_gmt":"2013-09-12T12:20:38","slug":"property-group-validation-attributes","status":"publish","type":"post","link":"https:\/\/www.minddriven.de\/index.php\/technology\/windows-8\/winrt\/property-group-validation-attributes","title":{"rendered":"WinRT XAML Validation library: Property-Group Validation Attributes"},"content":{"rendered":"<p>All predefined validation attributes in the <code>System.ComponentModel.DataAnnotations<\/code> namespace can be used to annotate a single property of an entity. So they cannot be used for validation that spans multiple properties.<\/p>\n<p>For this case, the <a href=\"https:\/\/winrtxamlvalidation.codeplex.com\/\">WinRT XAML Validation library<\/a> comes with the <code>GroupValidationAttribute <\/code>base class. Its member <code>CausativeProperties <\/code>can be used to define properties, which cause the validation to run, when their values are changing. With the member <code>AffectedProperties <\/code>you can set properties, that will be marked invalid when the validation of a property from the <code>CausativeProperties <\/code>group fails.<\/p>\n<p>The following code gives an example of a concrete validation class that extends the <code>GroupValidationAttribute <\/code>base class. <code>CausativeProperties <\/code>equals <code>AffectedProperties<\/code>, thus the group of properties that cause the validation equals the group of properties that are affected by the validation:<\/p>\n<pre class=\"brush:csharp;\">[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]\r\npublic sealed class ValidateAgainstMaxBidAttribute : GroupValidationAttribute\r\n{\r\n    protected override IEnumerable&lt;Expression&lt;Func&lt;object&gt;&gt;&gt; AffectedProperties\r\n    {\r\n        get\r\n        {\r\n            AuctionBid entity = null;\r\n            return new Expression&gt;[]\r\n                {\r\n                    () =&gt; entity.NewBid,\r\n                    () =&gt; entity.MaxNewBid\r\n                };\r\n        }\r\n    }\r\n\r\n    protected override IEnumerable&lt;Expression&lt;Func&lt;object&gt;&gt;&gt; CausativeProperties\r\n    {\r\n        get { return this.AffectedProperties; }\r\n    }\r\n\r\n    protected override ValidationResult IsValid(object value, ValidationContext validationContext)\r\n    {\r\n        var auctionBid = (AuctionBid)validationContext.ObjectInstance;\r\n\r\n        return auctionBid.MaxNewBid.HasValue &amp;&amp; auctionBid.NewBid &gt; auctionBid.MaxNewBid.Value\r\n            ? new ValidationResult(\"New bid must not be greater than highest bid.\")\r\n            : ValidationResult.Success;\r\n    }\r\n}<\/pre>\n<p>This group validation property can now be used to annotate the whole model entity on class level:<\/p>\n<pre class=\"brush:csharp;\">[ValidateAgainstMaxBid(ShowMessageOnProperty = false)]\r\npublic class AuctionBid : ValidationBindableBase\r\n{\r\n    public double CurrentBid { ... }\r\n    public double NewBid { ... }\r\n    public double? MaxNewBid { ... }\r\n}<\/pre>\n<p>Additionally, the <code>GroupValidationAttribute <\/code>class derives from the <code>ExtendedValidationAttribute <\/code>class, so you&#8217;re able to use additional functionality brought by the <code>ValidationLevel<\/code>, \u00a0<code>ShowMessageOnProperty <\/code>and <code>ShowMessageInSummary <\/code>properties and others.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>All predefined validation attributes in the System.ComponentModel.DataAnnotations namespace can be used to annotate a single property of an entity. So they cannot be used for validation that spans multiple properties. For this case, the WinRT XAML Validation library comes with the GroupValidationAttribute base class. Its member CausativeProperties can be used to define properties, which cause &hellip; <a href=\"https:\/\/www.minddriven.de\/index.php\/technology\/windows-8\/winrt\/property-group-validation-attributes\" class=\"more-link\"><span class=\"screen-reader-text\">WinRT XAML Validation library: Property-Group Validation Attributes<\/span> weiterlesen<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302,300],"tags":[309,310,308,143,322,76],"class_list":["post-1290","post","type-post","status-publish","format-standard","hentry","category-windows-8","category-winrt","tag-dot-net","tag-c-sharp","tag-user-input","tag-validation","tag-winrt","tag-xaml"],"_links":{"self":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/1290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/comments?post=1290"}],"version-history":[{"count":4,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/1290\/revisions"}],"predecessor-version":[{"id":1307,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/1290\/revisions\/1307"}],"wp:attachment":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/media?parent=1290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/categories?post=1290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/tags?post=1290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}