{"id":1302,"date":"2013-09-12T13:14:39","date_gmt":"2013-09-12T12:14:39","guid":{"rendered":"http:\/\/www.minddriven.de\/?p=1302"},"modified":"2013-09-12T13:21:00","modified_gmt":"2013-09-12T12:21:00","slug":"implicit-explicit-validation-execution","status":"publish","type":"post","link":"https:\/\/www.minddriven.de\/index.php\/technology\/windows-8\/winrt\/implicit-explicit-validation-execution","title":{"rendered":"WinRT XAML Validation library: Implicit and Explicit Validation Execution"},"content":{"rendered":"<p>Now that you have defined all your validation attributes on your model classes, you want to perform validation to detect if your entity is valid or not. Here you can go two ways with the <a href=\"http:\/\/www.minddriven.de\/winrtxamlvalidation.codeplex.com\" target=\"_blank\">WinRT XAML Validation library<\/a>: <em>implicit validation<\/em> validates your entity automatically when properties change, while with\u00a0<em>explicit validation<\/em> you call a function to perform the validation.<\/p>\n<h3>Validatable model classes<\/h3>\n<p>A prerequisite to perform validation on a model entity with the WinRT XAML Validation library is that your model class derives from the <code>ValidationBindableBase<\/code> class. This class stores all validation messages for you and is able to perform validation on your entity:<\/p>\n<pre style=\"brush: csharp;\">public class AuctionBid : ValidationBindableBase { ... }<\/pre>\n<h3>Implicit Validation Execution<\/h3>\n<p>Implicit validation is triggered automatically when an entity property changes, thus the setter is called. Implicit validation is an opt-in mechanism, meaning that you have to activate this functionality first before your entity gets validated. Just set the <code>IsImplicitValidationEnabled<\/code> property on your model entity that derives from <code>ValidationBindableBase<\/code> to <code>true<\/code>:<\/p>\n<pre class=\"brush:csharp;\">var bid = new AuctionBid();\r\nbid.IsImplicitValidationEnabled = true;<\/pre>\n<p>If you want to disable implicit validation execution temporarily, you are able to set <code>IsImplicitValidationEnabled=false<\/code> at any time. Finally for implicit validation to fire, you have to implement your model properties explicitly with a backing field. In the property&#8217;s setter, you&#8217;ve to call the <code>SetProperty()<\/code> method from the base class <code>ValidationBindableBase<\/code>, which performs the validation of the matching attributes for the property:<\/p>\n<pre class=\"brush:csharp;\">[ValidateAgainstMaxBid(ShowMessageOnProperty = false)]\r\npublic class AuctionBid : ValidationBindableBase\r\n{\r\n    private double newBid;\r\n\r\n    [Range(0, double.MaxValue, ErrorMessage = \"Value must be greater 0.\")]\r\n    [ValidateGreaterCurrentBid]\r\n    public double NewBid\r\n    {\r\n        get { return this.newBid; }\r\n        set { base.SetProperty(ref this.newBid, value); }\r\n    }\r\n\r\n    ...\r\n}<\/pre>\n<h3>Explicit Validation Execution<\/h3>\n<p>In many situations you don&#8217;t want to execute implicit validation everytime a property changes. For this case the WinRT XAML Validation library gives you the ability to execute validation explicitly by calling the <code>ValidationBindableBase.ValidateAsync()<\/code> method. Because <code>ValidationBindableBase<\/code> will be the base class for your model entities, that means calling the method on the entity itself:<\/p>\n<pre class=\"brush:csharp;\">var bid = new AuctionBid();\r\n...\r\nbool isValid = await bid.ValidateAsync();<\/pre>\n<p>The method returns <code>true<\/code> if the entity is valid and <code>false<\/code> otherwise. It&#8217;s <code>async<\/code> because of the <code>async<\/code> validation ability of attributes from the WinRT XAML Validation library.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now that you have defined all your validation attributes on your model classes, you want to perform validation to detect if your entity is valid or not. Here you can go two ways with the WinRT XAML Validation library: implicit validation validates your entity automatically when properties change, while with\u00a0explicit validation you call a function &hellip; <a href=\"https:\/\/www.minddriven.de\/index.php\/technology\/windows-8\/winrt\/implicit-explicit-validation-execution\" class=\"more-link\"><span class=\"screen-reader-text\">WinRT XAML Validation library: Implicit and Explicit Validation Execution<\/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-1302","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\/1302","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=1302"}],"version-history":[{"count":4,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/1302\/revisions"}],"predecessor-version":[{"id":1309,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/1302\/revisions\/1309"}],"wp:attachment":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/media?parent=1302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/categories?post=1302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/tags?post=1302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}