<?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>Technical Jargon &#187; FluentValidation</title>
	<atom:link href="http://www.jeremyskinner.co.uk/category/fluentvalidation/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jeremyskinner.co.uk</link>
	<description>Did you notice the information bar?</description>
	<lastBuildDate>Mon, 08 Feb 2010 16:18:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>FluentValidation C4mvc presentation</title>
		<link>http://www.jeremyskinner.co.uk/2010/02/08/fluentvalidation-c4mvc-presentation/</link>
		<comments>http://www.jeremyskinner.co.uk/2010/02/08/fluentvalidation-c4mvc-presentation/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 07:59:44 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[FluentValidation]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=355</guid>
		<description><![CDATA[
This Wednesday I&#8217;m going to be giving a live presentation on using FluentValidation for the c4mvc virtual usergroup.


The session will be live from 18:00 GMT (12:00 CST) and will last about 40 minutes. I&#8217;ll be covering the basics of using FluentValidation as well as how you can integrate it with ASP.NET MVC applications.
]]></description>
			<content:encoded><![CDATA[<p>
This Wednesday I&#8217;m going to be giving a live presentation on using <a href="http://fluentvalidation.codeplex.com">FluentValidation</a> for the <a href="http://www.c4mvc.net/">c4mvc virtual usergroup</a>.
</p>
<p>
The session will be live from 18:00 GMT (12:00 CST) and will last about 40 minutes. I&#8217;ll be covering the basics of using FluentValidation as well as how you can integrate it with ASP.NET MVC applications.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2010/02/08/fluentvalidation-c4mvc-presentation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>FluentValidation 1.2 Beta 2 and MVC2 RC2</title>
		<link>http://www.jeremyskinner.co.uk/2010/02/06/fluentvalidation-1-2-beta-2-and-mvc2-rc2/</link>
		<comments>http://www.jeremyskinner.co.uk/2010/02/06/fluentvalidation-1-2-beta-2-and-mvc2-rc2/#comments</comments>
		<pubDate>Sat, 06 Feb 2010 12:03:12 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[FluentValidation]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=343</guid>
		<description><![CDATA[
FluentValidation 1.2 beta 2 is now available to download. This release focuses mainly on integration with ASP.NET MVC 2.


Since my post on Limitations of MVC2&#8217;s ModelValidatorProviders ASP.NET MVC2 RC2 has been released which addresses some of the issues that I raised in the post.


Firstly, validation in MVC2 RC2 now performs model-validation rather than input validation. [...]]]></description>
			<content:encoded><![CDATA[<p>
FluentValidation 1.2 beta 2 <a href="http://fluentvalidation.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=40035">is now available to download</a>. This release focuses mainly on integration with ASP.NET MVC 2.
</p>
<p>
Since my post on <a href="http://www.jeremyskinner.co.uk/2010/01/13/limitations-of-mvc2s-modelvalidatorproviders/">Limitations of MVC2&#8217;s ModelValidatorProviders</a> ASP.NET MVC2 RC2 has been released which addresses some of the issues that I raised in the post.
</p>
<p>
Firstly, validation in MVC2 RC2 <a href="http://bradwilson.typepad.com/blog/2010/01/input-validation-vs-model-validation-in-aspnet-mvc.html">now performs model-validation rather than input validation</a>. This is much more in line with how FluentValidation works which makes the FluentValidation integration much more consistent with the built-in validation.
</p>
<p>
Secondly, it is now possible to disable the DataAnnotationsModelValidatorProvider&#8217;s &#8220;greedy&#8221; required rule. Out of the box, the DataAnnotationsModelValidatorProvider will *always* validate non-nullable value types, irrespective of whether the property is decorated with a [Required] attribute. This meant that you had to un-register the DataAnnotations provider if you didn&#8217;t want to get duplicate errors. It is now possible to turn this off by calling <strong>DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;</strong> in your application startup routine.
</p>
<p>
Finally, I added the ability to be able to execute some of FluentValidation&#8217;s Property Validators without needing to validate the entire object. This means it is now possible to stop the default &#8220;A value was required&#8221; message from being added to ModelState.
</p>
<h3>Enabling the FluentValidationModelValidatorProvider</h3>
<p>
After <a href="http://fluentvalidation.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=40035">downloading the 1.2 beta 2 release</a> you&#8217;ll need to add a reference to FluentValidation.dll and FluentValidation.Mvc.dll
</p>
<p>
To enable the FluentValidationModelValidatorProvider you will need to add it to the ModelValidatorProviders collection in your application startup routine:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> Application_Start<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	RegisterRoutes<span style="color: #000000;">&#40;</span>RouteTable.<span style="color: #0000FF;">Routes</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	DataAnnotationsModelValidatorProvider
		.<span style="color: #0000FF;">AddImplicitRequiredAttributeForValueTypes</span> <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
&nbsp;
	ModelValidatorProviders.<span style="color: #0000FF;">Providers</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>
		<span style="color: #008000;">new</span> FluentValidationModelValidatorProvider<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> AttributedValidatorFactory<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Note that you need to pass an IValidatorFactory to the FluentValidationModelValidatorProvider so that it knows how to create your validator instances. I&#8217;m using the AttributedValidatorFactory in this example, but in a real application you would probably use an implementation that wraps an IoC container instead.
</p>
<p>
Next, you can create your model object and associated validator:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>Validator<span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>PersonValidator<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Person <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Id <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Name <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Email <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Age <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> PersonValidator <span style="color: #008000;">:</span> AbstractValidator<span style="color: #008000;">&lt;</span>Person<span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> PersonValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Id</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">10</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Email</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">EmailAddress</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Age</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">InclusiveBetween</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">18</span>, <span style="color: #FF0000;">60</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Finally, you can create the controller and associated view:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> PeopleController <span style="color: #008000;">:</span> Controller <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> ActionResult Create<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#91;</span>AcceptVerbs<span style="color: #000000;">&#40;</span>HttpVerbs.<span style="color: #0000FF;">Post</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
	<span style="color: #0600FF;">public</span> ActionResult Create<span style="color: #000000;">&#40;</span>Person person<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span> ModelState.<span style="color: #0000FF;">IsValid</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Create&quot;</span>, person<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		TempData<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;notice&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Person successfully created&quot;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">return</span> RedirectToAction<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Index&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">ValidationSummary</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;%</span> using <span style="color: #006600; font-weight:bold;">&#40;</span>Html.<span style="color: #9900cc;">BeginForm</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #006600; font-weight:bold;">&#123;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
	Id: <span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">TextBoxFor</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Id</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">ValidationMessageFor</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Id</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
	&lt;br /&gt;
	Name: <span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">TextBoxFor</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Name</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">ValidationMessageFor</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Name</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>			
	&lt;br /&gt;
	Email: <span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span>Html.<span style="color: #9900cc;">TextBoxFor</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Email</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">ValidationMessageFor</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Email</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
	&lt;br /&gt;
	Age: <span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">TextBoxFor</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Age</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">ValidationMessageFor</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Age</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
&nbsp;
	&lt;br /&gt;&lt;br /&gt;
&nbsp;
	&lt;input type=&quot;submit&quot; value=&quot;submit&quot; /&gt;
<span style="color: #000000; font-weight: bold;">&lt;%</span> <span style="color: #006600; font-weight:bold;">&#125;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

<p>
Now when you post the form MVC&#8217;s DefaultModelBinder will validate the Person object using the FluentValidationModelValidatorProvider.
</p>
<h3>Clientside Validation</h3>
<p>
This release also has preliminary support for clientside validation. This is enabled by calling <strong>&lt;% Html.EnableClientValidation(); %&gt;</strong> in your views. At the moment, only the NotNull, NotEmpty, RegularExpression and Length validators are supported.
</p>
<p>
Note that if you try to use the clientside validation support with the default error messages, you will see error messages like this:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #666666;">'{PropertyName}'</span> must not be empty.</pre></div></div>

<p>
This is because FluentValidation uses templated error messages but the MVC Framework&#8217;s clientside validation runner does not know how to correctly format these. For now, the only workaround is to explicitly specify the error message:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Surname</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">WithMessage</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;'Surname' must not be empty&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>
For FluentValidation 1.3 I will consider providing a customised version of MVC&#8217;s MicrosoftMvcValidation.js that knows how to properly format FluentValidation error templates.
</p>
<h3>
Metadata Support<br />
</h3>
<p>
Out of the box, MVC2 allows you to use the DataAnnotations attributes to provide <a href="http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-2-modelmetadata.html">metadata for editor/display templates</a>. This release of FluentValidation allows you to use extra extension methods on your validator classes to define metadata using method chaining.
</p>
<p>
Firstly, you will need to replace the DataAnnotationsModelMetadataProvider with the new FluentValidationModelMetadataProvider in your Application_Start:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">ModelMetadataProviders.<span style="color: #0000FF;">Current</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FluentValidationModelMetadataProvider<span style="color: #000000;">&#40;</span>
	<span style="color: #008000;">new</span> AttributedValidatorFactory<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>
Now you can start using the metadata extension in your validator classes (you will need to import the FluentValidation.Mvc.MetadataExtensions namespace):
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> CustomerValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
   RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Surname</span><span style="color: #000000;">&#41;</span>
      .<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">//regular validator</span>
      .<span style="color: #0000FF;">UIHint</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Surname&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//MVC Metadata</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
The FluentValidationModelMetadataProvider currently supports the following metadata-related extension methods which correspond to the DataAnnotations attributes with the same name:</p>
<ul>
<li>HiddenInput</li>
<li>UIHint</li>
<li>Scaffold</li>
<li>DataType</li>
<li>DisplayName</li>
<li>DisplayFormat</li>
<li>ReadOnly</li>
</ul>
<p>
<strong>Edit: 8th Feb</strong>Note that there is a bug in the current beta build where MetaData won&#8217;t work in some circumstances. This is already fixed for the next beta, so you may want to use the latest binaries from <a href="http://build.jeremyskinner.co.uk/repository/download/bt2/.lastSuccessful/FluentValidation.zip?guest=1">the build server</a>.
</p>
<p>
As usual, binaries are <a href="http://fluentvalidation.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=40035">on CodePlex</a> and the source is <a href="http://github.com/JeremySkinner/FluentValidation">on GitHub</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2010/02/06/fluentvalidation-1-2-beta-2-and-mvc2-rc2/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>FluentValidation 1.2 beta 1 available</title>
		<link>http://www.jeremyskinner.co.uk/2010/01/23/fluentvalidation-1-2-beta-1-available/</link>
		<comments>http://www.jeremyskinner.co.uk/2010/01/23/fluentvalidation-1-2-beta-1-available/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 10:47:17 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[FluentValidation]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=283</guid>
		<description><![CDATA[
FluentValidation 1.2 beta 1 is now available to download.


This release contains several new features:

Setting the validator cascade mode

In v1.1, if you have multiple validators defined on a single property then all of those validators will be executed even if the first one fails. This can be changed by calling the Cascade method:


RuleFor&#40;x =&#62; x.Surname&#41;
  [...]]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://fluentvalidation.codeplex.com">FluentValidation</a> 1.2 beta 1 is <a href="http://fluentvalidation.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=39334">now available to download</a>.
</p>
<p>
This release contains several new features:
</p>
<h3>Setting the validator cascade mode</h3>
<p>
In v1.1, if you have multiple validators defined on a single property then all of those validators will be executed even if the first one fails. This can be changed by calling the Cascade method:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Surname</span><span style="color: #000000;">&#41;</span>
  .<span style="color: #0000FF;">Cascade</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">StopOnFirstFailure</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
  .<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
  .<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">250</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>
In this case, the Length validator would not be run on the Surname property if the NotNull validator fails.
</p>
<p>
This is customisable globally by setting the static property ValidatorOptions.CascadeMode.
</p>
<h3>MVC2 Support</h3>
<p>The FluentValidationModelBinder has been modified to work with ASP.NET MVC2 RC. Note that if you want to use FluentValidation with MVC1, you will need to use the older 1.1 release.</p>
<p>
FluentValidation 1.2 Beta 1 does not currently include the custom ModelMetadataProvider and ModelValidatorProvider that I mentioned <a href="http://www.jeremyskinner.co.uk/2009/12/20/fluentvalidation-moved-to-github-and-whats-coming-in-v1-2/">in this post</a>.
</p>
<p>In its current state, the MVC2 metadata API is not extensible enough to work well with FluentValidtion (see <a href="http://www.jeremyskinner.co.uk/2010/01/13/limitations-of-mvc2s-modelvalidatorproviders/">this post</a> for details) so I&#8217;ve omitted them from this beta. If MVC2&#8217;s support for external frameworks is improved before RTM, then I will consider re-adding these features for the next release.
</p>
<h3>Custom user state</h3>
<p>
Custom user state can be stored alongside validation failures:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">RuleFor<span style="color: #000000;">&#40;</span>person <span style="color: #008000;">=&gt;</span> person.<span style="color: #0000FF;">Surname</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">WithState</span><span style="color: #000000;">&#40;</span>person <span style="color: #008000;">=&gt;</span> <span style="color: #666666;">&quot;foo&quot;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>
This state can then be retrieved from the ValidationFailure object if the rule fails:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var result <span style="color: #008000;">=</span> validator.<span style="color: #0000FF;">Validate</span><span style="color: #000000;">&#40;</span>model<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var error <span style="color: #008000;">=</span> result.<span style="color: #0000FF;">Errors</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
var customState<span style="color: #008000;">=</span> error.<span style="color: #0000FF;">CustomState</span><span style="color: #008000;">;</span></pre></div></div>

<h3>OnAnyFailure</h3>
<p>
Rules can define a callback method that will be invoked if any of the validators associated with a particular rule fail:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">RuleFor<span style="color: #000000;">&#40;</span>person <span style="color: #008000;">=&gt;</span> person.<span style="color: #0000FF;">Surname</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">OnAnyFailure</span><span style="color: #000000;">&#40;</span>person <span style="color: #008000;">=&gt;</span> <span style="color: #000000;">&#123;</span>
   Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;#fail&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<h3>WithPropertyName</h3>
<p>The WithPropertyName method can be used to override the name of the property that will be used when associating an error message with a particular property:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> PersonValidator <span style="color: #008000;">:</span> AbstractValidator<span style="color: #008000;">&lt;</span>Person<span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">public</span> PersonValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    RuleFor<span style="color: #000000;">&#40;</span>person <span style="color: #008000;">=&gt;</span> person.<span style="color: #0000FF;">Surname</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">WithPropertyName</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;foo&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
var validator <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PersonValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var result <span style="color: #008000;">=</span> validator.<span style="color: #0000FF;">Validate</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> Person<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">//property name is 'foo' instead of 'Surname'</span>
<span style="color: #FF0000;">string</span> propertyName <span style="color: #008000;">=</span> result.<span style="color: #0000FF;">Errors</span>.<span style="color: #0000FF;">Single</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">PropertyName</span><span style="color: #008000;">;</span></pre></div></div>

<h3>InlineValidator</h3>
<p>
The InlineValidator can be used to define rules without creating a dedicated validator class:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var validator <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> InlineValidator<span style="color: #008000;">&lt;</span>Person<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
  v <span style="color: #008000;">=&gt;</span> v.<span style="color: #0000FF;">RuleFor</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Surname</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  v <span style="color: #008000;">=&gt;</span> v.<span style="color: #0000FF;">RuleFor</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Age</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">GreaterThan</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h3>ValidateAndThrow</h3>
<p>The ValidateAndThrow extension method can be used to throw an exception when validation fails rather than returning a ValidationResult.</p>
<h3>Experimental xVal integration</h3>
<p>
Thanks to <a href="http://schotime.net/blog/">Adam Schroder</a>, FluentValidation now includes experimental support for generating xVal rules. I&#8217;ve labelled this as &#8220;Experimental&#8221; as there is currently no support for custom validation messages or some of the more complex FluentValidation rules. The future of the FluentValidation xVal integration also depends on what happens with MVC2&#8217;s validation providers and whether xVal will continue to be developed after the MVC2 release.
</p>
<h3>New PropertyValidator base class (potentially breaking change)</h3>
<p>
In FluentValidation 1.x, all property validators implemented the IPropertyValidator&lt;T,TProperty&gt; interface. As of v1.2 this has been deprecated in favour of the PropertyValidator base class. Note that property validators themselves are no longer generic, which makes them easier to work with if you need to gather metadata about a validator.
</p>
<p>For example, in v1.1 the NotNullValidator looked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>ValidationMessage<span style="color: #000000;">&#40;</span>Key <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;notnull_error&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> NotNullValidator<span style="color: #008000;">&lt;</span>T, TProperty<span style="color: #008000;">&gt;</span> <span style="color: #008000;">:</span> IPropertyValidator<span style="color: #008000;">&lt;</span>T, TProperty<span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> PropertyValidatorResult Validate<span style="color: #000000;">&#40;</span>PropertyValidatorContext<span style="color: #008000;">&lt;</span>T, TProperty<span style="color: #008000;">&gt;</span> context<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>context.<span style="color: #0000FF;">PropertyValue</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			var formatter <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MessageFormatter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AppendProperyName</span><span style="color: #000000;">&#40;</span>context.<span style="color: #0000FF;">PropertyDescription</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #FF0000;">string</span> error <span style="color: #008000;">=</span> context.<span style="color: #0000FF;">GetFormattedErrorMessage</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>NotNullValidator<span style="color: #008000;">&lt;</span>T, TProperty<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#41;</span>, formatter<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
			<span style="color: #0600FF;">return</span> PropertyValidatorResult.<span style="color: #0000FF;">Failure</span><span style="color: #000000;">&#40;</span>error<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0600FF;">return</span> PropertyValidatorResult.<span style="color: #0000FF;">Success</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>&#8230;while in v1.2 it now looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> NotNullValidator <span style="color: #008000;">:</span> PropertyValidator, INotNullValidator <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> NotNullValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">:</span> <span style="color: #0600FF;">base</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> Messages.<span style="color: #0000FF;">notnull_error</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		SupportsStandaloneValidation <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">bool</span> IsValid<span style="color: #000000;">&#40;</span>PropertyValidatorContext context<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>context.<span style="color: #0000FF;">PropertyValue</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0600FF;">return</span> false<span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
The use of a base class helps keep the validator definitions simple. Also note that the ValidationMessage attribute has been removed &#8211; localised error messages can now be specified either by passing a resource type and key to the base PropertyValidator constructor or by using a lambda expression.
</p>
<p>
If you have written any custom property validators, it is recommended that you change them to inherit from PropertyValidator. However, <strong>there is a backwards compatibility layer in place which should allow old-style property validators to continue working.</strong>
</p>
<h3>DefaultResourceManager has been removed (breaking change)</h3>
<p>
In order to add better localization support, the DefaultResourceManager has been removed in favour of Visual Studio&#8217;s default generated &#8220;Messages&#8221; class. If you need to replace this with your own Messages class, you can set the static ResourceProviderType property on the ValidatorOptions class.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2010/01/23/fluentvalidation-1-2-beta-1-available/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Limitations of MVC2&#8217;s ModelValidatorProviders</title>
		<link>http://www.jeremyskinner.co.uk/2010/01/13/limitations-of-mvc2s-modelvalidatorproviders/</link>
		<comments>http://www.jeremyskinner.co.uk/2010/01/13/limitations-of-mvc2s-modelvalidatorproviders/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 11:08:40 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[FluentValidation]]></category>
		<category><![CDATA[aspnetmvc]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=274</guid>
		<description><![CDATA[
One of the new features in ASP.NET MVC 2 is the ability the ability to define multiple validation providers. These are used by the DefaultModelBinder to automatically validate your model objects when they are passed to action parameters.


I wanted to try and write one of these for FluentValidation (see this post) but after trying this [...]]]></description>
			<content:encoded><![CDATA[<p>
One of the new features in ASP.NET MVC 2 is the ability the ability to define multiple validation providers. These are used by the DefaultModelBinder to automatically validate your model objects when they are passed to action parameters.
</p>
<p>
I wanted to try and write one of these for <a href="http://fluentvalidation.codeplex.com">FluentValidation</a> (see <a href="http://www.jeremyskinner.co.uk/2009/12/20/fluentvalidation-moved-to-github-and-whats-coming-in-v1-2/">this post</a>) but after trying this it has become quite clear that the ModelValidatorProvider API does have some limitations which have caused me some problems.
</p>
<h3>Problem 1: Property-level validation</h3>
<p>
To understand this problem, first we need to look at how different validation frameworks perform their work.
</p>
<p>
When using FluentValidation, the validators work against a pre-populated object. For example:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CustomerValidator <span style="color: #008000;">:</span> AbstractValidator<span style="color: #008000;">&lt;</span>Customer<span style="color: #000000;">&#123;</span> 
  <span style="color: #0600FF;">public</span> CustomerValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
     RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Surname</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
     RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">CreditLimit</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">GreaterThanOrEqualTo</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
var customer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Customer <span style="color: #000000;">&#123;</span> CreditLimit <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span>, Surname <span style="color: #008000;">=</span> <span style="color: #0600FF;">null</span> <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
var validator <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CustomerValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var results <span style="color: #008000;">=</span> validator.<span style="color: #0000FF;">Validate</span><span style="color: #000000;">&#40;</span>customer<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>
You cannot reliably validate individual properties by themselves because they may have dependencies on *other* properties being set. For example:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Surname</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotEqual</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Forename</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>
This rule says tht the Surname and Forename properties must not be equal. In this case, you cannot validate the surname property by itself without having first set the value for the Forename property. Other validation frameworks (eg the Castle Project&#8217;s validation attributes) work in a similar way.
</p>
<p>
The MVC Framework&#8217;s DefaultModelBinder is primarily designed to work with the System.ComponentModel.DataAnnotations attributes. These work in a different way &#8211; they validate individual values, not the entire object.  To support this, the DefaultModelBinder allows validating both the individual property values (in the OnPropertyValidated method) as well as validating the entire instance (in the OnModelUpdated method).
</p>
<p>
Here are the relevant methods from DefaultModelBinder:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">//OnPropertyValidated - called for each property being bound.</span>
<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">virtual</span> <span style="color: #0600FF;">void</span> OnPropertyValidated<span style="color: #000000;">&#40;</span>ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, <span style="color: #FF0000;">object</span> value<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    ModelMetadata propertyMetadata <span style="color: #008000;">=</span> bindingContext.<span style="color: #0000FF;">PropertyMetadata</span><span style="color: #000000;">&#91;</span>propertyDescriptor.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
    propertyMetadata.<span style="color: #0000FF;">Model</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
    <span style="color: #FF0000;">string</span> modelStateKey <span style="color: #008000;">=</span> CreateSubPropertyName<span style="color: #000000;">&#40;</span>bindingContext.<span style="color: #0000FF;">ModelName</span>, propertyMetadata.<span style="color: #0000FF;">PropertyName</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// run validation</span>
    <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>ModelValidator validator <span style="color: #0600FF;">in</span> propertyMetadata.<span style="color: #0000FF;">GetValidators</span><span style="color: #000000;">&#40;</span>controllerContext<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>ModelValidationResult validationResult <span style="color: #0600FF;">in</span> validator.<span style="color: #0000FF;">Validate</span><span style="color: #000000;">&#40;</span>bindingContext.<span style="color: #0000FF;">Model</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            bindingContext.<span style="color: #0000FF;">ModelState</span>.<span style="color: #0000FF;">AddModelError</span><span style="color: #000000;">&#40;</span>CreateSubPropertyName<span style="color: #000000;">&#40;</span>modelStateKey, validationResult.<span style="color: #0000FF;">MemberName</span><span style="color: #000000;">&#41;</span>, validationResult.<span style="color: #0000FF;">Message</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// only add a &quot;value was required&quot; error message if there were no validation errors</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>bindingContext.<span style="color: #0000FF;">ModelState</span>.<span style="color: #0000FF;">IsValidField</span><span style="color: #000000;">&#40;</span>modelStateKey<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>value <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span>TypeHelpers.<span style="color: #0000FF;">TypeAllowsNullValue</span><span style="color: #000000;">&#40;</span>propertyDescriptor.<span style="color: #0000FF;">PropertyType</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            bindingContext.<span style="color: #0000FF;">ModelState</span>.<span style="color: #0000FF;">AddModelError</span><span style="color: #000000;">&#40;</span>modelStateKey, GetValueRequiredResource<span style="color: #000000;">&#40;</span>controllerContext<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">//OnModelUpdated - called once the entire object has been bound. </span>
<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">virtual</span> <span style="color: #0600FF;">void</span> OnModelUpdated<span style="color: #000000;">&#40;</span>ControllerContext controllerContext, ModelBindingContext bindingContext<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    IDataErrorInfo errorProvider <span style="color: #008000;">=</span> bindingContext.<span style="color: #0000FF;">Model</span> <span style="color: #0600FF;">as</span> IDataErrorInfo<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>errorProvider <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
         <span style="color: #FF0000;">string</span> errorText <span style="color: #008000;">=</span> errorProvider.<span style="color: #0000FF;">Error</span><span style="color: #008000;">;</span>
         <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>errorText<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            bindingContext.<span style="color: #0000FF;">ModelState</span>.<span style="color: #0000FF;">AddModelError</span><span style="color: #000000;">&#40;</span>bindingContext.<span style="color: #0000FF;">ModelName</span>, errorText<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
          <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>IsModelValid<span style="color: #000000;">&#40;</span>bindingContext<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        return<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>ModelValidator validator <span style="color: #0600FF;">in</span> bindingContext.<span style="color: #0000FF;">ModelMetadata</span>.<span style="color: #0000FF;">GetValidators</span><span style="color: #000000;">&#40;</span>controllerContext<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>ModelValidationResult validationResult <span style="color: #0600FF;">in</span> validator.<span style="color: #0000FF;">Validate</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            bindingContext.<span style="color: #0000FF;">ModelState</span>.<span style="color: #0000FF;">AddModelError</span><span style="color: #000000;">&#40;</span>CreateSubPropertyName<span style="color: #000000;">&#40;</span>bindingContext.<span style="color: #0000FF;">ModelName</span>, validationResult.<span style="color: #0000FF;">MemberName</span><span style="color: #000000;">&#41;</span>, validationResult.<span style="color: #0000FF;">Message</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
On first glance, this looks like it will work. When OnPropertyValidated calls GetValidators, I can return an empty collection of validators and do all of my work in a single validator in OnModelUpdated. Unfortunately, this does not work.
</p>
<p>
See the end of OnPropertyValidated? Here, it checks if there have been any validation errors for the property and if not then it <strong>will go ahead and run its own built-in field validation anyway for non-nullable value types</strong> &#8211; it will put a message saying &#8220;A value was required&#8221; into ModelState. And you can&#8217;t turn this off. The *only way* to stop this built-in validation is to support property-level validation in your custom ValidatorProvider. Not very useful if the underlying framework does not support this.
</p>
<p>
To work around the above issue, I ended up adding property-level validation to FluentValidation, but this is not a completely reliable solution because of the issues related to cross-property validation I mentioned above.
</p>
<h3>Problem 2: The DataAnnotationsModelValidatorProvider is greedy</h3>
<p>
The next problem that I ran into is that the DataAnnotationsModelValidatorProvider will always run required field validation, even if you&#8217;re not using the DataAnnotations attributes!
</p>
<p>Here is an example that uses FluentValidation to validate an object where a null value is being bound to a non-nullable value type:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>Validator<span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>FooValidator<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Foo <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Id <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> FooValidator <span style="color: #008000;">:</span> AbstractValidator<span style="color: #008000;">&lt;</span>Foo<span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> FooValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Id</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotEmpty</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AddsOneError<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	var foo <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Foo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	ModelValidatorProviders.<span style="color: #0000FF;">Providers</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>
		<span style="color: #008000;">new</span> FluentValidationModelValidatorProvider<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> AttributedValidatorFactory<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	var meta <span style="color: #008000;">=</span> ModelMetadataProviders.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">GetMetadataForProperty</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #0600FF;">null</span>, <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>Foo<span style="color: #000000;">&#41;</span>, <span style="color: #666666;">&quot;Id&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	var validators <span style="color: #008000;">=</span> ModelValidatorProviders.<span style="color: #0000FF;">Providers</span>.<span style="color: #0000FF;">GetValidators</span><span style="color: #000000;">&#40;</span>meta, <span style="color: #008000;">new</span> ControllerContext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	var results <span style="color: #008000;">=</span> validators.<span style="color: #0000FF;">SelectMany</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Validate</span><span style="color: #000000;">&#40;</span>foo<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>, results.<span style="color: #0000FF;">Count</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//fails - 2 instead of 1</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
This test fails because there are two error messages rather than one:
</p>
<pre>The Id field is required.
'Id' should not be empty.
</pre>
<p>
&#8230;the first of these errors comes from the DataAnnotationsModelValidatorProvider <strong>even though I have NOT decorated the Id property with a [Required] attribute</strong>.
</p>
<p>
Why does this happen? The answer is in DataAnnotationsModelValidatorProvider&#8217;s GetValidators method:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"> <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>metadata.<span style="color: #0000FF;">IsRequired</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span>attributes.<span style="color: #0000FF;">Any</span><span style="color: #000000;">&#40;</span>a <span style="color: #008000;">=&gt;</span> a <span style="color: #008000;">is</span> RequiredAttribute<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
       attributes <span style="color: #008000;">=</span> attributes.<span style="color: #0000FF;">Concat</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> <span style="color: #008000;">new</span> RequiredAttribute<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #000000;">&#125;</span></pre></div></div>

<p>
This checks to see if the property is a non-nullable type and it does not have a RequiredAttribute, <strong>then it assumes that the property should have a RequiredAttribute anyway!</strong><br />
You can work around this by removing the DataAnnotationsModelValidatorProvider:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">ModelValidatorProviders.<span style="color: #0000FF;">Providers</span>.<span style="color: #0000FF;">Clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
ModelValidatorProviders.<span style="color: #0000FF;">Providers</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>
	<span style="color: #008000;">new</span> FluentValidationModelValidatorProvider<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> AttributedValidatorFactory<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>
&#8230;and the test now passes.
</p>
<p>
Unfortunately, this is far from ideal. This means you cannot use the DataAnnotations attributes to validate one model and a custom provider for a different model. You have to choose one or the other.
</p>
<p>
One way to work around this problem is to use a composite validator provider that wraps the underlying providers:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CompositeValidatorProvider <span style="color: #008000;">:</span> ModelValidatorProvider <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Bootstrap<span style="color: #000000;">&#40;</span>ModelValidatorProviderCollection providerCollection, <span style="color: #0600FF;">params</span> ModelValidatorProvider<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> customProviders<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		var validators <span style="color: #008000;">=</span> customProviders.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		validators.<span style="color: #0000FF;">AddRange</span><span style="color: #000000;">&#40;</span>providerCollection<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		providerCollection.<span style="color: #0000FF;">Clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		providerCollection.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> CompositeValidatorProvider<span style="color: #000000;">&#40;</span>validators<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">readonly</span> IEnumerable<span style="color: #008000;">&lt;</span>ModelValidatorProvider<span style="color: #008000;">&gt;</span> innerInnerProviders<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">protected</span> CompositeValidatorProvider<span style="color: #000000;">&#40;</span>IEnumerable<span style="color: #008000;">&lt;</span>ModelValidatorProvider<span style="color: #008000;">&gt;</span> innerProviders<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">innerInnerProviders</span> <span style="color: #008000;">=</span> innerProviders<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> IEnumerable<span style="color: #008000;">&lt;</span>ModelValidator<span style="color: #008000;">&gt;</span> GetValidators<span style="color: #000000;">&#40;</span>ModelMetadata metadata, ControllerContext context<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span>var provider <span style="color: #0600FF;">in</span> innerInnerProviders<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			var validators <span style="color: #008000;">=</span> provider.<span style="color: #0000FF;">GetValidators</span><span style="color: #000000;">&#40;</span>metadata, context<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>validators.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0600FF;">return</span> validators<span style="color: #008000;">;</span>	
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF;">return</span> Enumerable.<span style="color: #0000FF;">Empty</span><span style="color: #008000;">&lt;</span>ModelValidator<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This custom provider can be used to wrap the built-in providers (including the DataAnnotations provider) as well as your own custom providers. In its GetValidators method, it iterates over each of the inner providers and asks each one to create a collection of validators. <strong>It only returns validators from the first provider that returns a non-empty collection</strong>. This means that if your custom provider can validate the object then the DataAnnotations provider will not be executed.</p>
<p>
Now, the following test passes:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AddsOneError<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	var foo <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Foo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    	var fluentProvider <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FluentValidationModelValidatorProvider<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> AttributedValidatorFactory<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	CompositeValidatorProvider.<span style="color: #0000FF;">Bootstrap</span><span style="color: #000000;">&#40;</span>ModelValidatorProviders.<span style="color: #0000FF;">Providers</span>, fluentProvider<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	var meta <span style="color: #008000;">=</span> ModelMetadataProviders.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">GetMetadataForProperty</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #0600FF;">null</span>, <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>Foo<span style="color: #000000;">&#41;</span>, <span style="color: #666666;">&quot;Id&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	var validators <span style="color: #008000;">=</span> ModelValidatorProviders.<span style="color: #0000FF;">Providers</span>.<span style="color: #0000FF;">GetValidators</span><span style="color: #000000;">&#40;</span>meta, <span style="color: #008000;">new</span> ControllerContext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	var results <span style="color: #008000;">=</span> validators.<span style="color: #0000FF;">SelectMany</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Validate</span><span style="color: #000000;">&#40;</span>foo<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>, results.<span style="color: #0000FF;">Count</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
This raises the question: <strong>if you have to resort to doing this, what is the point in allowing multiple validation providers to be registered in the first place?</strong>
</p>
<p>
I am really hoping that the MVC team will fix this fairly significant problem before RTM, but as MVC2 is already at the RC stage I think this will be unlikely.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2010/01/13/limitations-of-mvc2s-modelvalidatorproviders/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>FluentValidation moved to GitHub and what&#8217;s coming in v1.2</title>
		<link>http://www.jeremyskinner.co.uk/2009/12/20/fluentvalidation-moved-to-github-and-whats-coming-in-v1-2/</link>
		<comments>http://www.jeremyskinner.co.uk/2009/12/20/fluentvalidation-moved-to-github-and-whats-coming-in-v1-2/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 22:19:18 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[FluentValidation]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=265</guid>
		<description><![CDATA[After several months of increasing frustration with CodePlex&#8217;s svn integration, I have moved the source code for FluentValidation to GitHub.

Continuous builds can still be downloaded from my build server.


Documentation, discussions and the issue tracker will still remain on the CodePlex site, but if you want to play with the source code then you&#8217;ll need to [...]]]></description>
			<content:encoded><![CDATA[<p>After several months of increasing frustration with <a href="http://www.codeplex.com/">CodePlex&#8217;s</a> svn integration, I have moved the source code for <a href="http://fluentvalidation.codeplex.com">FluentValidation</a> to <a href="http://github.com/JeremySkinner/FluentValidation">GitHub</a>.</p>
<p>
Continuous builds can still be downloaded <a href="http://build.jeremyskinner.co.uk">from my build server.</a>
</p>
<p>
Documentation, discussions and the issue tracker will still remain on the CodePlex site, but if you want to play with the source code then you&#8217;ll need to <a href="http://github.com/JeremySkinner/FluentValidation">head over to GitHub</a> instead. If you have not used Git before then I recommend reading my <a href="http://mvccontrib.github.com/MvcContrib/">Git Guide</a> (the guide was written for the MvcContrib project, but all the concepts apply to FluentValidation too).
</p>
<h2>What&#8217;s coming with v1.2?</h2>
<p>
The upcoming release of FluentValidation (v1.2) will have several changes including the following:
</p>
<h3>A custom ModelMetadataProvider</h3>
<p>
The FluentValidationModelMetadataProvider will allow you to define ASP.NET MVC Metadata inside your validator classes, eg:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> CustomerValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
   RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Surname</span><span style="color: #000000;">&#41;</span>
      .<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">//regular validator</span>
      .<span style="color: #0000FF;">UIHint</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Surname&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//MVC Metadata</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
The FluentValidationModelMetadataProvider will support the following metadata-related extension methods:</p>
<ul>
<li>HiddenInput</li>
<li>UIHint</li>
<li>Scaffold</li>
<li>DataType</li>
<li>DisplayName</li>
<li>DisplayFormat</li>
<li>ReadOnly</li>
</ul>
<h3>A custom ModelValidatorProvider</h3>
<p>
Previous versions of FluentValidation had a custom IModelBinder implementation that allowed you to use FluentValidation to validate ASP.NET MVC action parameters. This will no longer be necessary in v1.2 as ASP.NET MVC 2&#8217;s ModelValidatorProvider infrastructure means FluentValidation can now be plugged in to MVC&#8217;s DefaultModelBinder:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MvcApplication <span style="color: #008000;">:</span> HttpApplication <span style="color: #000000;">&#123;</span>
   <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Application_Start<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      RegisterRoutes<span style="color: #000000;">&#40;</span>RouteTable.<span style="color: #0000FF;">Routes</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
      ModelValidatorProviders.<span style="color: #0000FF;">Providers</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> FluentValidationModelValidatorProvider<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> AttributedValidatorFactory<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Note that the FluentValidationModelValidatorProvider will only support server-side validation, not client-side validation. FluentValidation&#8217;s validation mechanism is not compatible with how ASP.NET MVC 2 generates clientside validation rules.
</p>
<h3>Customisable Validator Cascade Mode</h3>
<p>
The validator cascade mode will be customisable in v1.2. Currently, if you have multiple validators defined on a single property then all of those validators will be executed even if the first one fails. This can be changed by calling the Cascade method:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Surname</span><span style="color: #000000;">&#41;</span>
  .<span style="color: #0000FF;">Cascade</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">StopOnFirstFailure</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
  .<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
  .<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">250</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>
In this case, the Length validator would not be run on the Surname property if the NotNull validator fails.
</p>
<p>
This will also be customisable globally by setting the static property ValidatorOptions.CascadeMode.
</p>
<p>
I hope to release a first beta of v1.2 in the next couple of weeks. The final release should coincide with the release of ASP.NET MVC 2.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2009/12/20/fluentvalidation-moved-to-github-and-whats-coming-in-v1-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FluentValidation with DotNetNuke</title>
		<link>http://www.jeremyskinner.co.uk/2009/11/15/fluentvalidation-with-dotnetnuke/</link>
		<comments>http://www.jeremyskinner.co.uk/2009/11/15/fluentvalidation-with-dotnetnuke/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 09:15:23 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[FluentValidation]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=241</guid>
		<description><![CDATA[Michael Washington has posted an article about using FluentValidation with DotNetNuke.

Be sure to check it out.
]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.dotnetnuke.com/Community/Blogs/tabid/825/BlogID/77/Default.aspx">Michael Washington</a> has posted an article about using <a href="http://fluentvalidation.codeplex.com">FluentValidation</a> with DotNetNuke.</p>
<p>
Be sure to <a href="http://www.dotnetnuke.com/Community/Blogs/tabid/825/EntryId/2413/DotNetNuke-and-Fluent-Validation.aspx">check it out.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2009/11/15/fluentvalidation-with-dotnetnuke/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FluentValidation, MVC and TDD</title>
		<link>http://www.jeremyskinner.co.uk/2009/08/19/fluentvalidation-mvc-and-tdd/</link>
		<comments>http://www.jeremyskinner.co.uk/2009/08/19/fluentvalidation-mvc-and-tdd/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 16:25:05 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[FluentValidation]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=182</guid>
		<description><![CDATA[
Richard Dingwall recently wrote a post about using FluentValidation when doing TDD with ASP.NET MVC. Go check it out!
]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://richarddingwall.name/2009/08/19/asp-net-mvc-tdd-and-fluent-validation/">Richard Dingwall</a> recently wrote a post about using <a href="http://fluentvalidation.codeplex.com">FluentValidation</a> when doing TDD with ASP.NET MVC. <a href="http://richarddingwall.name/2009/08/19/asp-net-mvc-tdd-and-fluent-validation/">Go check it out!</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2009/08/19/fluentvalidation-mvc-and-tdd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FluentValidation 1.1 Released</title>
		<link>http://www.jeremyskinner.co.uk/2009/07/18/fluentvalidation-1-1-released/</link>
		<comments>http://www.jeremyskinner.co.uk/2009/07/18/fluentvalidation-1-1-released/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 12:23:47 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[FluentValidation]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=152</guid>
		<description><![CDATA[
FluentValidation version 1.1 is now available to download.

Quick recap: What is FluentValidation?
FluentValidation allows you to use a fluent interface and lambda expressions to define validation rules for your objects:

public class CustomerValidator: AbstractValidator&#60;Customer&#62; &#123;
  public CustomerValidator&#40;&#41; &#123;
    RuleFor&#40;customer =&#62; customer.Surname&#41;.NotEmpty&#40;&#41;;
    RuleFor&#40;customer =&#62; customer.Forename&#41;.NotEmpty&#40;&#41;.WithMessage&#40;&#34;Please specify a first name&#34;&#41;;
   [...]]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://fluentvalidation.codeplex.com">FluentValidation</a> version 1.1 is now <a href="http://fluentvalidation.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=30284">available to download.</a>
</p>
<h2>Quick recap: What is FluentValidation?</h2>
<p>FluentValidation allows you to use a fluent interface and lambda expressions to define validation rules for your objects:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CustomerValidator<span style="color: #008000;">:</span> AbstractValidator<span style="color: #008000;">&lt;</span>Customer<span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">public</span> CustomerValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    RuleFor<span style="color: #000000;">&#40;</span>customer <span style="color: #008000;">=&gt;</span> customer.<span style="color: #0000FF;">Surname</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotEmpty</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    RuleFor<span style="color: #000000;">&#40;</span>customer <span style="color: #008000;">=&gt;</span> customer.<span style="color: #0000FF;">Forename</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotEmpty</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">WithMessage</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Please specify a first name&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    RuleFor<span style="color: #000000;">&#40;</span>customer <span style="color: #008000;">=&gt;</span> customer.<span style="color: #0000FF;">Discount</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotEqual</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">When</span><span style="color: #000000;">&#40;</span>customer <span style="color: #008000;">=&gt;</span> customer.<span style="color: #0000FF;">HasDiscount</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    RuleFor<span style="color: #000000;">&#40;</span>customer <span style="color: #008000;">=&gt;</span> customer.<span style="color: #0000FF;">Address</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">20</span>, <span style="color: #FF0000;">250</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    RuleFor<span style="color: #000000;">&#40;</span>customer <span style="color: #008000;">=&gt;</span> customer.<span style="color: #0000FF;">Postcode</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Must</span><span style="color: #000000;">&#40;</span>BeAValidPostcode<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">WithMessage</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Please specify a valid postcode&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">bool</span> BeAValidPostcode<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> postcode<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// custom postcode validating logic goes here</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
&#8230;which can then be executed against a particular instance of your class:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Customer customer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Customer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
CustomerValidator validator <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CustomerValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
ValidationResult results <span style="color: #008000;">=</span> validator.<span style="color: #0000FF;">Validate</span><span style="color: #000000;">&#40;</span>customer<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #FF0000;">bool</span> validationSucceeded <span style="color: #008000;">=</span> results.<span style="color: #0000FF;">IsValid</span><span style="color: #008000;">;</span>
IList<span style="color: #008000;">&lt;</span>ValidationFailure<span style="color: #008000;">&gt;</span> failures <span style="color: #008000;">=</span> results.<span style="color: #0000FF;">Errors</span><span style="color: #008000;">;</span></pre></div></div>

<h2>What&#8217;s new in version 1.1?</h2>
<p>
Quite a lot of FluentValidation has been re-written with the 1.1 release, but these are the changes most noticable to the end user:
</p>
<h3>
Nested Validators<br />
</h3>
<p>
It is now possible to re-use validators. For example, if you have a Customer object that has a property of type Address, then you can declare an AddressValidator like this:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> AddressValidator <span style="color: #008000;">:</span> AbstractValidator<span style="color: #008000;">&lt;</span>Address<span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#123;</span>
   <span style="color: #0600FF;">public</span> AddressValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Property</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">250</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">District</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">250</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Country</span><span style="color: #000000;">&#41;</span>...
        <span style="color: #008080; font-style: italic;">//etc</span>
   <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>&#8230;and then your CustomerValidator can specify that the Address property should be validated using the AddressValidator:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CustomerValidator <span style="color: #008000;">:</span> AbstractValidator<span style="color: #008000;">&lt;</span>Customer<span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> CustomerValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
       RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Surname</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
       RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Address</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">SetValidator</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> AddressValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> 
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h3>Additional Language Translations</h3>
<p>
Thanks to several user contributions, the default validation messages are now available in German, Spanish, French, Dutch and Portuguese in addition to English.
</p>
<h3>Chaining Validators</h3>
<p>
In version 1.0, if you wanted to specify multiple validation rules on a single property then you had to use the &#8216;And&#8217; operator:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">And</span>.<span style="color: #0000FF;">NotEqual</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;foo&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>&#8230;but in v1.1 these can now be chained together directly:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotEqual</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;foo&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<h3>IValidatorSelector</h3>
<p>
The new IValidatorSelector interface allows you to specify which validation rules should be executed when performing validation. I&#8217;m planning a more in-depth post on this in the future.
</p>
<h3>Message Formatting Placeholders</h3>
<p>
Custom validation messages can now use special placeholders, eg:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Surname</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">WithMessage</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;You must enter a value for {PropertyName}!&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>There&#8217;s a complete list of placeholders <a href="http://fluentvalidation.codeplex.com/Wiki/View.aspx?title=Validators">in the documentation</a>. I plan to write a more detailed post about how to use custom placeholders in your own, custom validators in the future.</p>
<h2>What&#8217;s coming in v1.2?</h2>
<p>
For version 1.2, I&#8217;m planning on incorporating <a href="http://schotime.net/blog/index.php/2009/06/13/fluentvalidation-xval-integration/">Adam&#8217;s work</a> on adding <a href="http://xval.codeplex.com/">xVal</a> integration for FluentValidation as well as adding a sample application.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2009/07/18/fluentvalidation-1-1-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using FluentValidation with ASP.NET MVC Opinionated Input Builders</title>
		<link>http://www.jeremyskinner.co.uk/2009/06/14/using-fluentvalidation-with-asp-net-mvc-opinionated-input-builders/</link>
		<comments>http://www.jeremyskinner.co.uk/2009/06/14/using-fluentvalidation-with-asp-net-mvc-opinionated-input-builders/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 14:27:28 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[FluentValidation]]></category>
		<category><![CDATA[aspnetmvc]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=136</guid>
		<description><![CDATA[
I&#8217;ve been following Eric Hexter&#8217;s series on using opinionated input builders with ASP.NET MVC and wanted to see whether it would be possible to use his InputBuilder code together with FluentValidation.


In Eric&#8217;s example, he has a class called SampleModel whose properties are decorated by several attributes:


public class SampleModel
&#123;
  public Guid Guid &#123; get; set; [...]]]></description>
			<content:encoded><![CDATA[<p>
I&#8217;ve been following Eric Hexter&#8217;s series on <a href="http://www.lostechies.com/blogs/hex/archive/2009/06/09/opinionated-input-builders-for-asp-net-mvc-using-partials-part-i.aspx">using opinionated input builders with ASP.NET MVC</a> and wanted to see whether it would be possible to use his InputBuilder code together with <a href="http://fluentvalidation.codeplex.com">FluentValidation</a>.
</p>
<p>
In Eric&#8217;s example, he has a class called <strong>SampleModel</strong> whose properties are decorated by several attributes:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> SampleModel
<span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">public</span> Guid Guid <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #000000;">&#91;</span>Required<span style="color: #000000;">&#93;</span>
   <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Name <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #000000;">&#91;</span>Required<span style="color: #000000;">&#93;</span>
  <span style="color: #000000;">&#91;</span>Label<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Number of Types&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
  <span style="color: #000000;">&#91;</span>PartialView<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;RadioButtons&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
  <span style="color: #0600FF;">public</span> NumberOfTypeEnum EnumAsRadioButton <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> NumberOfTypeEnum <span style="color: #FF0000;">Enum</span> <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #000000;">&#91;</span>Range<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">3</span>, <span style="color: #FF0000;">15</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> IntegerRangeValue <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #000000;">&#91;</span>Example<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;mm/dd/yyyy hh:mm PM&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
  <span style="color: #0600FF;">public</span> DateTime TimeStamp <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #000000;">&#91;</span>DataType<span style="color: #000000;">&#40;</span>DataType.<span style="color: #0000FF;">MultilineText</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Html <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> IsNeeded <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
These properties on this object are then rendered in the view by using the strongly-typed <strong>Input</strong> extension:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%=</span>Html.<span style="color: #0000FF;">Input</span><span style="color: #000000;">&#40;</span>c <span style="color: #008000;">=&gt;</span> c.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">%&gt;</span>
<span style="color: #008000;">&lt;%=</span>Html.<span style="color: #0000FF;">Input</span><span style="color: #000000;">&#40;</span>c <span style="color: #008000;">=&gt;</span> c.<span style="color: #0000FF;">TimeStamp</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">%&gt;</span>
<span style="color: #008000;">&lt;%=</span>Html.<span style="color: #0000FF;">Input</span><span style="color: #000000;">&#40;</span>c <span style="color: #008000;">=&gt;</span> c.<span style="color: #0000FF;">Html</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">%&gt;</span>
...<span style="color: #0000FF;">etc</span>...</pre></div></div>

<p>The attributes that decorate the properties drive the UI, so for example decorating the Html property with [DataType(DataType.MultilineText)] would cause a multi-line textbox to be rendered.</p>
<p>
Rather than using attributes, my aim was to configure the InputBuilders by storing metadata alongside validation information in a Validator class.
</p>
<p>
Out of the box, FluentValidation supports several validator types. Some of these map nicely to the DataAnnotations attributes used by the SampleModel. For example, the [Required] attribute maps nicely to the NotNull and NotEmpty validators, and the [Range] attribute works nicely with the GreaterThan/LessThan validators:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> SampleModelValidator <span style="color: #008000;">:</span> AbstractValidator<span style="color: #008000;">&lt;</span>SampleModel<span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">public</span> SampleModelValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotEmpty</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">IntegerRangeValue</span><span style="color: #000000;">&#41;</span>
      .<span style="color: #0000FF;">GreaterThanOrEqualTo</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span>
      .<span style="color: #0000FF;">LessThanOrEqualTo</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">15</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
However, there are no equivalents for the metadata attributes such as [PartialView], [Example] or [DataType], but these can easily be built by constructing some &#8216;fake&#8217; validators (these validators won&#8217;t actually perform any validation &#8211; they will simply hold metadata).
</p>
<p>
For example, to build an equivalent for the DataTypeAttribute, we can create a property validator like this:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> IDataTypeMetaData <span style="color: #000000;">&#123;</span>
	DataType DataType <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> DataTypeValidator<span style="color: #008000;">&lt;</span>T, TProperty<span style="color: #008000;">&gt;</span> <span style="color: #008000;">:</span> IPropertyValidator<span style="color: #008000;">&lt;</span>T, TProperty<span style="color: #008000;">&gt;</span>, IDataTypeMetaData <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> DataType datatype<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> DataTypeValidator<span style="color: #000000;">&#40;</span>DataType datatype<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">datatype</span> <span style="color: #008000;">=</span> datatype<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> PropertyValidatorResult Validate<span style="color: #000000;">&#40;</span>PropertyValidatorContext<span style="color: #008000;">&lt;</span>T, TProperty<span style="color: #008000;">&gt;</span> context<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> PropertyValidatorResult.<span style="color: #0000FF;">Success</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> DataType DataType <span style="color: #000000;">&#123;</span>
		get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> datatype<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Note that we declare both a validator to hold the DataType information and also a non-generic interface (which we&#8217;ll use later to obtain the metadata). The Validate method is a no-op, always reporting success (as we don&#8217;t actually want to perform any validation).
</p>
<p>
Next, we can declare an extension method that allows this to be used from within our SampleModelValidator:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> ValidatorExtensions <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> IRuleBuilderOptions<span style="color: #008000;">&lt;</span>T, TProperty<span style="color: #008000;">&gt;</span> DataType<span style="color: #008000;">&lt;</span>T, TProperty<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> IRuleBuilder<span style="color: #008000;">&lt;</span>T, TProperty<span style="color: #008000;">&gt;</span> ruleBuilder, DataType dataType<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> ruleBuilder.<span style="color: #0000FF;">SetValidator</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> DataTypeValidator<span style="color: #008000;">&lt;</span>T, TProperty<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>dataType<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
After using this approach to create equivalents for all the other metadata attributes, our SampleModelValidator now looks like this:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> SampleModelValidator <span style="color: #008000;">:</span> AbstractValidator<span style="color: #008000;">&lt;</span>SampleModel<span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> SampleModelValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotEmpty</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">EnumAsRadioButton</span><span style="color: #000000;">&#41;</span>
			.<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			.<span style="color: #0000FF;">RenderUsing</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;RadioButtons&quot;</span><span style="color: #000000;">&#41;</span>
			.<span style="color: #0000FF;">WithName</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Number of Types&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">IntegerRangeValue</span><span style="color: #000000;">&#41;</span>
			.<span style="color: #0000FF;">GreaterThanOrEqualTo</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span>
			.<span style="color: #0000FF;">LessThanOrEqualTo</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">15</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">TimeStamp</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Example</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;mm/dd/yyyy hh:mm PM&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		RuleFor<span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Html</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">DataType</span><span style="color: #000000;">&#40;</span>DataType.<span style="color: #0000FF;">MultilineText</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
The next stage is to create a custom <strong>ValidatorDescriptor</strong>. The default ValidatorDescriptor doesn&#8217;t do very much so we&#8217;ll need to inherit from it and create some additional methods to retrieve the information needed by the InputBuilder:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Reflection</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">FluentValidation</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">FluentValidation.Internal</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">FluentValidation.Validators</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> ICustomValidatorDescriptor <span style="color: #000000;">&#123;</span>
	<span style="color: #FF0000;">string</span> GetExample<span style="color: #000000;">&#40;</span>PropertyInfo prop<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #FF0000;">string</span> GetLabel<span style="color: #000000;">&#40;</span>PropertyInfo prop<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #FF0000;">string</span> GetPartialName<span style="color: #000000;">&#40;</span>PropertyInfo prop<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #FF0000;">bool</span> GetIsRequired<span style="color: #000000;">&#40;</span>PropertyInfo prop<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CustomValidatorDescriptor<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #008000;">:</span> ValidatorDescriptor<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>, ICustomValidatorDescriptor <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> CustomValidatorDescriptor<span style="color: #000000;">&#40;</span>IEnumerable<span style="color: #008000;">&lt;</span>IValidationRule<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> ruleBuilders<span style="color: #000000;">&#41;</span> <span style="color: #008000;">:</span> <span style="color: #0600FF;">base</span><span style="color: #000000;">&#40;</span>ruleBuilders<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> GetExample<span style="color: #000000;">&#40;</span>PropertyInfo prop<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> Rules.<span style="color: #0000FF;">OfType</span><span style="color: #008000;">&lt;</span>ISimplePropertyRule<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		       	.<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Member</span> <span style="color: #008000;">==</span> prop<span style="color: #000000;">&#41;</span>
		       	.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Validator</span><span style="color: #000000;">&#41;</span>
		       	.<span style="color: #0000FF;">OfType</span><span style="color: #008000;">&lt;</span>IExampleMetaData<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		       	.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Example</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">FirstOrDefault</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">??</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> GetLabel<span style="color: #000000;">&#40;</span>PropertyInfo prop<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> Rules.<span style="color: #0000FF;">OfType</span><span style="color: #008000;">&lt;</span>IPropertyRule<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
				.<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Member</span> <span style="color: #008000;">==</span> prop<span style="color: #000000;">&#41;</span>
				.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">PropertyDescription</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">FirstOrDefault</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> GetPartialName<span style="color: #000000;">&#40;</span>PropertyInfo prop<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> Rules.<span style="color: #0000FF;">OfType</span><span style="color: #008000;">&lt;</span>ISimplePropertyRule<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
				.<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Member</span> <span style="color: #008000;">==</span> prop<span style="color: #000000;">&#41;</span>
				.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Validator</span><span style="color: #000000;">&#41;</span>
				.<span style="color: #0000FF;">OfType</span><span style="color: #008000;">&lt;</span>IRenderUsingMetaData<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
				.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">ViewName</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">FirstOrDefault</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> GetIsRequired<span style="color: #000000;">&#40;</span>PropertyInfo prop<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> Rules.<span style="color: #0000FF;">OfType</span><span style="color: #008000;">&lt;</span>ISimplePropertyRule<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			.<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Member</span> <span style="color: #008000;">==</span> prop<span style="color: #000000;">&#41;</span>
			.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Validator</span><span style="color: #000000;">&#41;</span>
			.<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x <span style="color: #008000;">is</span> INotNullValidator <span style="color: #008000;">||</span> x <span style="color: #008000;">is</span> INotEmptyValidator<span style="color: #000000;">&#41;</span>
			.<span style="color: #0000FF;">Any</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
The ValidatorDescriptor accepts in its constructor a collection of IValidationRule objects that represent the rules configured in the SampleModelValidator. Using some Linq magic we can retrieve the metadata information that we stored inside the validation rules. We also have to override the <strong>CreateDescriptor</strong> method in our SampleModelValidator to return an instance of our custom ValidatorDescriptor, rather than the default:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> IValidatorDescriptor<span style="color: #008000;">&lt;</span>SampleModel<span style="color: #008000;">&gt;</span> CreateDescriptor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> CustomValidatorDescriptor<span style="color: #008000;">&lt;</span>SampleModel<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
The next stage is to override the InputBuilder&#8217;s default conventions so that it will use our SampleModelValidator to obtain its metadata. To do this, we create a <strong>FluentValidationConventions</strong> class that accepts a ValidatorFactory in its constructor and contains methods that delegate to our CustomValidatorDescriptor:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> FluentValidationConventions <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> IValidatorFactory validatorFactory<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> FluentValidationConventions<span style="color: #000000;">&#40;</span>IValidatorFactory factory<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">validatorFactory</span> <span style="color: #008000;">=</span> factory<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">private</span> ICustomValidatorDescriptor GetDescriptor<span style="color: #000000;">&#40;</span>Type type<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		var validator <span style="color: #008000;">=</span> validatorFactory.<span style="color: #0000FF;">GetValidator</span><span style="color: #000000;">&#40;</span>type<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>ICustomValidatorDescriptor<span style="color: #000000;">&#41;</span> validator.<span style="color: #0000FF;">CreateDescriptor</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> ExampleConvention<span style="color: #000000;">&#40;</span>PropertyInfo prop<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> GetDescriptor<span style="color: #000000;">&#40;</span>prop.<span style="color: #0000FF;">ReflectedType</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">GetExample</span><span style="color: #000000;">&#40;</span>prop<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> LabelConvention<span style="color: #000000;">&#40;</span>PropertyInfo prop<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> GetDescriptor<span style="color: #000000;">&#40;</span>prop.<span style="color: #0000FF;">ReflectedType</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">GetLabel</span><span style="color: #000000;">&#40;</span>prop<span style="color: #000000;">&#41;</span> <span style="color: #008000;">??</span> DefaultConventions.<span style="color: #0000FF;">LabelForProperty</span><span style="color: #000000;">&#40;</span>prop<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> PartialNameConvention<span style="color: #000000;">&#40;</span>PropertyInfo prop<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> GetDescriptor<span style="color: #000000;">&#40;</span>prop.<span style="color: #0000FF;">ReflectedType</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">GetPartialName</span><span style="color: #000000;">&#40;</span>prop<span style="color: #000000;">&#41;</span> <span style="color: #008000;">??</span> DefaultConventions.<span style="color: #0000FF;">PartialName</span><span style="color: #000000;">&#40;</span>prop<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> RequiredConvention<span style="color: #000000;">&#40;</span>PropertyInfo prop<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> GetDescriptor<span style="color: #000000;">&#40;</span>prop.<span style="color: #0000FF;">ReflectedType</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">GetIsRequired</span><span style="color: #000000;">&#40;</span>prop<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Note that I used a very simple implementation of ValidatorFactory for this example that wraps a Dictionary. A real ValidatorFactory would probably wrap an IoC container or a Service Locator:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> SampleValidatorFactory <span style="color: #008000;">:</span> IValidatorFactory <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">static</span> Dictionary<span style="color: #008000;">&lt;</span>Type, IValidator<span style="color: #008000;">&gt;</span> validators <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span>Type, IValidator<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #000000;">&#123;</span> <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>SampleModel<span style="color: #000000;">&#41;</span>, <span style="color: #008000;">new</span> SampleModelValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> IValidator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> GetValidator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>IValidator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#41;</span> GetValidator<span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span> <span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> IValidator GetValidator<span style="color: #000000;">&#40;</span>Type type<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		IValidator validator<span style="color: #008000;">;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>validators.<span style="color: #0000FF;">TryGetValue</span><span style="color: #000000;">&#40;</span>type, <span style="color: #0600FF;">out</span> validator<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0600FF;">return</span> validator<span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0600FF;">return</span> null<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
In our global.asax, we can then replace the InputBuilder&#8217;s default conventions with our custom conventions:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> Application_Start<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	RegisterRoutes<span style="color: #000000;">&#40;</span>RouteTable.<span style="color: #0000FF;">Routes</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	InputBuilder.<span style="color: #0000FF;">InputBuilder</span>.<span style="color: #0000FF;">BootStrap</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	SetupFluentValidationConventions<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> SetupFluentValidationConventions<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	var conventions <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FluentValidationConventions<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> SampleValidatorFactory<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	ModelPropertyFactory.<span style="color: #0000FF;">ExampleForPropertyConvention</span> <span style="color: #008000;">=</span> conventions.<span style="color: #0000FF;">ExampleConvention</span><span style="color: #008000;">;</span>
	ModelPropertyFactory.<span style="color: #0000FF;">LabelForPropertyConvention</span> <span style="color: #008000;">=</span> conventions.<span style="color: #0000FF;">LabelConvention</span><span style="color: #008000;">;</span>
	ModelPropertyFactory.<span style="color: #0000FF;">PartialNameConvention</span> <span style="color: #008000;">=</span> conventions.<span style="color: #0000FF;">PartialNameConvention</span><span style="color: #008000;">;</span> 
	ModelPropertyFactory.<span style="color: #0000FF;">PropertyIsRequiredConvention</span> <span style="color: #008000;">=</span> conventions.<span style="color: #0000FF;">RequiredConvention</span><span style="color: #008000;">;</span> 
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
&#8230;and that&#8217;s it. The InputBuilder will now get all its metadata from our Validator class rather than from attributes. If you want to play with the source code for this example it&#8217;s available <del datetime="2009-07-20T19:53:40+00:00">in my svn repository at <strong>https://www.jeremyskinner.co.uk/svn/public/trunk/FluentValidationInputBuilders</strong> (username &#8216;guest&#8217;)</del> on <a href="http://github.com/JeremySkinner/Experiments/tree/master">GitHub</a> and can be <a href="http://cloud.github.com/downloads/JeremySkinner/Experiments/FluentValidationInputBuilders.zip">downloaded from here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2009/06/14/using-fluentvalidation-with-asp-net-mvc-opinionated-input-builders/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>FluentValidation 0.2 Released</title>
		<link>http://www.jeremyskinner.co.uk/2008/12/20/fluentvalidation-02-released/</link>
		<comments>http://www.jeremyskinner.co.uk/2008/12/20/fluentvalidation-02-released/#comments</comments>
		<pubDate>Sat, 20 Dec 2008 14:04:44 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[FluentValidation]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=83</guid>
		<description><![CDATA[The second release of my validation library, FluentValidation, is now available for download.
Changes in this version:

 Added Silverlight compatibility
 Moved support for the CommonServiceLocator into a separate assembly 
 Adding additional interfaces for some of the validators (ILengthValidator, INotNullValidator)
 AbstractValidator now implements IEnumerable&#60;IRuleBuilder&#60;T&#62;&#62;
 Added ShouldHaveValidationError extension method for testing validators

]]></description>
			<content:encoded><![CDATA[<p>The second release of my validation library, FluentValidation, is now <a href="http://fluentvalidation.codeplex.com">available for download.</a></p>
<p>Changes in this version:</p>
<ul>
<li> Added Silverlight compatibility</li>
<li> Moved support for the CommonServiceLocator into a separate assembly </li>
<li> Adding additional interfaces for some of the validators (ILengthValidator, INotNullValidator)</li>
<li> AbstractValidator now implements IEnumerable&lt;IRuleBuilder&lt;T&gt;&gt;</li>
<li> Added ShouldHaveValidationError extension method for testing validators</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2008/12/20/fluentvalidation-02-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
