<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Schotime.net &#187; Validation</title>
	<atom:link href="http://schotime.net/blog/index.php/tag/validation/feed/" rel="self" type="application/rss+xml" />
	<link>http://schotime.net/blog</link>
	<description>All Things .Net and Me</description>
	<lastBuildDate>Sun, 20 Nov 2011 01:44:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>FluentValidation Xval Integration</title>
		<link>http://schotime.net/blog/index.php/2009/06/13/fluentvalidation-xval-integration/</link>
		<comments>http://schotime.net/blog/index.php/2009/06/13/fluentvalidation-xval-integration/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 14:00:00 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Fluent Validation]]></category>
		<category><![CDATA[Validation]]></category>
		<category><![CDATA[xVal]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2009/06/13/fluentvalidation-xval-integration/</guid>
		<description><![CDATA[After a few months of using FluentValidation I asked its author Jeremy Skinner if it were possible to integrate this with xVal. At that time it was not possible because there were no easy way to access the properties needed by xVal. After submitting a few patches, we now have a solution which enables xVal [...]]]></description>
			<content:encoded><![CDATA[<p>After a few months of using <a href="http://fluentvalidation.codeplex.com" target="_blank" onclick="pageTracker._trackPageview('/outgoing/fluentvalidation.codeplex.com?referer=');">FluentValidation</a> I asked its author Jeremy Skinner if it were possible to integrate this with <a href="http://xval.codeplex.com" target="_blank" onclick="pageTracker._trackPageview('/outgoing/xval.codeplex.com?referer=');">xVal</a>. At that time it was not possible because there were no easy way to access the properties needed by xVal. After submitting a few patches, we now have a solution which enables xVal integration with most of the FV validators.</p>
<p>It currently supports the following FV validatiors:</p>
<ul>
<li>NullValidator </li>
<li>NotEmptyValidator </li>
<li>LengthValidator </li>
<li>RegularExpressionValidator </li>
<li>ComparisonValidator including:
<ul>
<li>Equal </li>
<li>Not Equal </li>
<li>Greater Than or Equal </li>
<li>Less Than or Equal </li>
</ul>
</li>
</ul>
<p>To configure the integration we need to tell xVal to use the FV rules provider rather than the default one. This is done in the global.asax.cs in Application_Start().</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td style="background: black" valign="top" width="400">
<pre class="code"><span style="background: black; color: white">xVal.</span><span style="background: black; color: #ffc66d">ActiveRuleProviders</span><span style="background: black; color: white">.Providers.Clear();
xVal.</span><span style="background: black; color: #ffc66d">ActiveRuleProviders</span><span style="background: black; color: white">.Providers.Add(
    </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: white">FluentValidation.xValIntegration.
            </span><span style="background: black; color: #ffc66d">FluentValidationRulesProvider</span><span style="background: black; color: white">(</span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">AttributedValidatorFactory</span><span style="background: black; color: white">()));</span></pre>
</td>
</tr>
</tbody>
</table>
<p>The rules provider here is instructed to use the AttributedValidatorFactory to instruct the provider to use the attribute attached to the model class to find the validation class for that model.</p>
<p>Note: This is still new and xVal is still in beta so there may be some issues. If you find any please let me know so we can fix them as soon as possible.</p>
<p>Hopefully once its ready it can be checked in with the other providers at the xVal codeplex site.</p>
<p>This is currently in the development source code which can be downloaded and tried now.</p>
<p>Cheers,</p>
<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2009/06/13/fluentvalidation-xval-integration/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Fluent Validation Model Binder &#8211; Asp.net MVC</title>
		<link>http://schotime.net/blog/index.php/2009/05/11/fluent-validation-model-binder-aspnet-mvc/</link>
		<comments>http://schotime.net/blog/index.php/2009/05/11/fluent-validation-model-binder-aspnet-mvc/#comments</comments>
		<pubDate>Mon, 11 May 2009 13:59:00 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Fluent Validation]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2009/05/11/fluent-validation-model-binder-aspnet-mvc/</guid>
		<description><![CDATA[A few weeks ago I found the Fluent Validation framework by Jeremy Skinner. I needed to conditionally validate a model depending on an application setting. eg. Description field mandatory / not mandatory depending on the clients business requirements. I loved the simplicity of the framework and the separation from the model it provided. Since then [...]]]></description>
			<content:encoded><![CDATA[<p>A few weeks ago I found the <a href="http://fluentvalidation.codeplex.com/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/fluentvalidation.codeplex.com/?referer=');">Fluent Validation</a> framework by <a href="http://www.jeremyskinner.co.uk/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.jeremyskinner.co.uk/?referer=');">Jeremy Skinner</a>. I needed to conditionally validate a model depending on an application setting. eg. Description field mandatory / not mandatory depending on the clients business requirements. I loved the simplicity of the framework and the separation from the model it provided.</p>
<p>Since then I have submitted a few patches for the framework, one of which is the Fluent Validation Model binder. Inspired by the Data Annotations Model binder, it works in much the same way. Once you have it set to your default model binder, it will validate any model which contains the specific attribute. This will become clear in the examples below.</p>
<p>Firstly lets take our model.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td style="background: black" valign="top" width="400" code?="code?"><span style="background: black; color: white">           <br /> 
<pre class="code"><span style="background: black; color: white">    [</span><span style="background: black; color: #ffc66d">Validator</span><span style="background: black; color: white">(</span><span style="background: black; color: #cc7832">typeof</span><span style="background: black; color: white">(</span><span style="background: black; color: #ffc66d">LineItemValidator</span><span style="background: black; color: white">))]
    </span><span style="background: black; color: #cc7832">public class </span><span style="background: black; color: #ffc66d">LineItem
    </span><span style="background: black; color: white">{
        </span><span style="background: black; color: #cc7832">public int </span><span style="background: black; color: white">LineNumber { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
        </span><span style="background: black; color: #cc7832">public </span><span style="background: black; color: #6897bb">DateTime </span><span style="background: black; color: white">Date { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
        </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">Description { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
        </span><span style="background: black; color: #cc7832">public decimal </span><span style="background: black; color: white">Net { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
        </span><span style="background: black; color: #cc7832">public decimal </span><span style="background: black; color: white">Tax { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
        </span><span style="background: black; color: #cc7832">public decimal </span><span style="background: black; color: white">Gross { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
    }</span></pre>
<p>          <a href="http://11011.net/software/vspaste" onclick="pageTracker._trackPageview('/outgoing/11011.net/software/vspaste?referer=');"></a></p>
<p></span></td>
</tr>
</tbody>
</table>
<p>Attached to this simple LineItem class is a Validator attribute. This attribute is used by the Model Binder to locate the Class used for validation. Below I will define my LineItemValidator class which will hold the rules for the validation.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td style="background: black" valign="top" width="400" code?="code?">
<pre><span style="background: black; color: white">    </span><span style="background: black; color: #cc7832">public class </span><span style="background: black; color: #ffc66d">LineItemValidator </span><span style="background: black; color: white">: </span><span style="background: black; color: #ffc66d">AbstractValidator</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #ffc66d">LineItem</span><span style="background: black; color: white">&gt;
    {
        </span><span style="background: black; color: #cc7832">public </span><span style="background: black; color: white">LineItemValidator()
        {
            RuleFor(x =&gt; x.Description)
                .NotEmpty().When(x =&gt; </span><span style="background: black; color: #ffc66d">Settings</span><span style="background: black; color: white">.DescriptionRequired)
                .And
                .Length(</span><span style="background: black; color: #6897bb">0</span><span style="background: black; color: white">, </span><span style="background: black; color: #6897bb">30</span><span style="background: black; color: white">);

            RuleFor(x =&gt; x.Date)
                .GreaterThanOrEqualTo(</span><span style="background: black; color: #6897bb">DateTime</span><span style="background: black; color: white">.Today);

            RuleFor(x =&gt; x.Net)
                .GreaterThan(</span><span style="background: black; color: #6897bb">0</span><span style="background: black; color: white">);

            RuleFor(x =&gt; x.Gross)
                .GreaterThan(</span><span style="background: black; color: #6897bb">0</span><span style="background: black; color: white">)
                .And
                .Equal(y =&gt; (y.Net + y.Tax))
                .WithName(</span><span style="background: black; color: #a5c25c">&quot;Total Amount&quot;</span><span style="background: black; color: white">);
        }
    }</span></pre>
<p>        <a href="http://11011.net/software/vspaste" onclick="pageTracker._trackPageview('/outgoing/11011.net/software/vspaste?referer=');"></a></td>
</tr>
</tbody>
</table>
<p>As you can see the class must inherit from AbstractValidator&lt;T&gt; where T is the model you want to define the rules for. The first rule uses the conditional When clause where it will only validate that the field is NotEmpty when the Settings.DescriptionRequired boolean is true. Also, another thing is the complex validation taking place on the Gross field. Not only does it validate that it is greater than 0, but that the value is equal to the net + tax amount. This is very elegant indeed. I have also specified the ‘WithName’ clause which has also been integrated into the model binder so that if an error occurs with the gross field, the WithName value will be displayed when an error happens. This is extremely handy for language localization or when the name of the field on the Model is insufficient. </p>
<p>Wiring this Model binder up in the Application_start event in the global.asax.cs is as easy as this.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td style="background: black" valign="top" width="400" code?="code?">
<pre><span style="background: black; color: white">     </span><span style="background: black; color: #ffc66d">ModelBinders</span><span style="background: black; color: white">.Binders.DefaultBinder =
                </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">FluentValidationModelBinder</span><span style="background: black; color: white">(</span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">AttributedValidatorFactory</span><span style="background: black; color: white">());</span></pre>
</td>
</tr>
</tbody>
</table>
<p>Note that we have to pass an instance of the AttributedValidatoryFactory into the Model Binder. This means if you have an alternate way of locating the Validator class other than via the attribute you can inherit from IValidatorFactory and create your own.</p>
<p>Now when a parameter of LineItem gets passed into a controller it will be validated against the model and all errors placed into the ModelState. This can then check the isValid property to determine if there are any errors and proceed accordingly.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td style="background: black" valign="top" width="400"><span style="background: black; color: white"><br />
          </p>
<pre class="code"><span style="background: black; color: white">        [</span><span style="background: black; color: #ffc66d">AcceptVerbs</span><span style="background: black; color: white">(</span><span style="background: black; color: #6897bb">HttpVerbs</span><span style="background: black; color: white">.Post)]
        </span><span style="background: black; color: #cc7832">public </span><span style="background: black; color: #ffc66d">ActionResult </span><span style="background: black; color: white">Edit(</span><span style="background: black; color: #ffc66d">List</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #ffc66d">LineItem</span><span style="background: black; color: white">&gt; lineItems)
        {
            </span><span style="background: black; color: #cc7832">if </span><span style="background: black; color: white">(ModelState.IsValid)
                </span><span style="background: black; color: #cc7832">return </span><span style="background: black; color: white">RedirectToAction(</span><span style="background: black; color: #a5c25c">&quot;Edit&quot;</span><span style="background: black; color: white">);

            </span><span style="background: black; color: #cc7832">return </span><span style="background: black; color: white">View();
        }</span></pre>
<p>        </span></td>
</tr>
</tbody>
</table>
<p>And that’s it. I hope you enjoy using the Fluent Validation framework as much as I have and happy coding. For all you guys waiting for xVal integration, I will try and post a solution in the coming week. </p>
</p>
<p>Adam
  </p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2009/05/11/fluent-validation-model-binder-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Integrating xVal Validation with Linq-to-Sql</title>
		<link>http://schotime.net/blog/index.php/2009/03/31/integrating-xval-validation-with-linq-to-sql/</link>
		<comments>http://schotime.net/blog/index.php/2009/03/31/integrating-xval-validation-with-linq-to-sql/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 13:58:00 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Validation]]></category>
		<category><![CDATA[xVal]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2009/03/31/integrating-xval-validation-with-linq-to-sql/</guid>
		<description><![CDATA[In a previous post I showed you how you can use xVal and the IDataErrorInfo class to add validation to your Asp.net MVC website. In this post I will extend that to Linq-to-Sql and the classes it generates. The northwind database has a suppliers table. The info contained below is using that table with linq-to-sql. [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous <a href="http://schotime.net/blog/index.php/2009/03/05/validation-with-aspnet-mvc-xval-idataerrorinfo/" onclick="pageTracker._trackPageview('/outgoing/schotime.net/blog/index.php/2009/03/05/validation-with-aspnet-mvc-xval-idataerrorinfo/?referer=');">post</a> I showed you how you can use xVal and the IDataErrorInfo class to add validation to your Asp.net MVC website. In this post I will extend that to Linq-to-Sql and the classes it generates.</p>
<p>The northwind database has a suppliers table. The info contained below is using that table with linq-to-sql. </p>
<p>After adding the table to the designer, a Supplier class gets constructed in the background. This class is a partial class which means we can add to it without changing the code auto-generated in the designer.cs file.</p>
<p>We can then create a partial class called Supplier inheriting from Custom Validation and add a MetadataType attribute to it. This attribute specifies the class for which use for validating the Supplier class.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td style="background: black" valign="top" width="400">
<pre class="code"><span style="background: black; color: white">    [</span><span style="background: black; color: #ffc66d">MetadataType</span><span style="background: black; color: white">(</span><span style="background: black; color: #cc7832">typeof</span><span style="background: black; color: white">(</span><span style="background: black; color: #ffc66d">SupplierValidation</span><span style="background: black; color: white">))]
</span><span style="background: black; color: #cc7832">    public partial class </span><span style="background: black; color: #ffc66d">Supplier</span><span style="background: black; color: white"> : </span><span style="background: black; color: #ffc66d">CustomValidation</span><span style="background: black; color: #ffc66d">
</span><span style="background: black; color: white">    {
    }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>We can then create the SupplierValidation class specifying the properties of the Supplier class we would like to be validated. For instance here I only want to validate the ContactName and the ContactTitle of the Supplier.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td style="background: black" valign="top" width="400">
<pre class="code"><span style="background: black; color: white">    </span><span style="background: black; color: #cc7832">    public class </span><span style="background: black; color: #ffc66d">SupplierValidation
    </span><span style="background: black; color: white">{
        [</span><span style="background: black; color: #ffc66d">Required</span><span style="background: black; color: white">]
        </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">ContactName { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }

        [</span><span style="background: black; color: #ffc66d">Required</span><span style="background: black; color: white">, </span><span style="background: black; color: #ffc66d">Range</span><span style="background: black; color: white">(</span><span style="background: black; color: #6897bb">0</span><span style="background: black; color: white">, </span><span style="background: black; color: #6897bb">10</span><span style="background: black; color: white">)]
        </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">ContactTitle { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
    }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>This specifies that both fields are required and that the ContactTitle cannot be more than 10 characters in length. </p>
<p>The other benefit of using the buddy class here is that if you need to regenerate a table in the linq-to-sql designer, you won’t lose your changes because they’re contained in a separate file.</p>
<p>From here when a Supplier gets passed to in as a parameter on a Controller action it will be validated using the rules in the Supplier Validation class. </p>
<p>Cheers,<br />
  <br />Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2009/03/31/integrating-xval-validation-with-linq-to-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

