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

<channel>
	<title>Schotime.net &#187; c# mvc validation asp.net</title>
	<atom:link href="http://schotime.net/blog/index.php/tag/c-mvc-validation-asp-net/feed/" rel="self" type="application/rss+xml" />
	<link>http://schotime.net/blog</link>
	<description>All Things .Net and Me</description>
	<lastBuildDate>Thu, 01 Jul 2010 14:42:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Custom Validation Attributes</title>
		<link>http://schotime.net/blog/index.php/2009/03/11/custom-validation-attributes/</link>
		<comments>http://schotime.net/blog/index.php/2009/03/11/custom-validation-attributes/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 13:57:00 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[c# mvc validation asp.net]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2009/03/11/custom-validation-attributes/</guid>
		<description><![CDATA[In my last post I showed you how to use Validation attributes on your model for validation. Today I will show how you can create your own attributes that can provide custom validation for you. To accomplish this we need to inherit from ValidationAttribute. We&#8217;ll create an attribute today that will only allow a &#34;Yes&#34; [...]]]></description>
			<content:encoded><![CDATA[<p>In my last <a href="http://schotime.net/blog/index.php/2009/03/05/validation-with-aspnet-mvc-xval-idataerrorinfo/">post</a> I showed you how to use Validation attributes on your model for validation. Today I will show how you can create your own attributes that can provide custom validation for you.    </p>
<p>To accomplish this we need to inherit from ValidationAttribute. We&#8217;ll create an attribute today that will only allow a &quot;Yes&quot; or &quot;No&quot; value to be entered. A pretty bad example however it will still suffice.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td style="background: black" valign="top" width="400"> 
<pre class="code"><span style="background: black; color: #cc7832">public class </span><span style="background: black; color: #ffc66d">TwoValueAttribute </span><span style="background: black; color: white">: </span><span style="background: black; color: #ffc66d">ValidationAttribute
</span><span style="background: black; color: white">{
    </span><span style="background: black; color: #cc7832">private string </span><span style="background: black; color: white">str1 { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
    </span><span style="background: black; color: #cc7832">private string </span><span style="background: black; color: white">str2 { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }

    </span><span style="background: black; color: #cc7832">public </span><span style="background: black; color: white">TwoValueAttribute(</span><span style="background: black; color: #cc7832">string </span><span style="background: black; color: white">str1, </span><span style="background: black; color: #cc7832">string </span><span style="background: black; color: white">str2)
    {
        </span><span style="background: black; color: #cc7832">this</span><span style="background: black; color: white">.str1 = str1;
        </span><span style="background: black; color: #cc7832">this</span><span style="background: black; color: white">.str2 = str2;
    }

    </span><span style="background: black; color: #cc7832">public override bool </span><span style="background: black; color: white">IsValid(</span><span style="background: black; color: #cc7832">object </span><span style="background: black; color: white">value)
    {
        </span><span style="background: black; color: #cc7832">if </span><span style="background: black; color: white">(value.ToString() == str1 || value.ToString() == str2)
            </span><span style="background: black; color: #cc7832">return true</span><span style="background: black; color: white">;
        </span><span style="background: black; color: #cc7832">else
            return false</span><span style="background: black; color: white">;
    }

    </span><span style="background: black; color: #cc7832">public override string </span><span style="background: black; color: white">FormatErrorMessage(</span><span style="background: black; color: #cc7832">string </span><span style="background: black; color: white">name)
    {
        </span><span style="background: black; color: #cc7832">return string</span><span style="background: black; color: white">.Format(</span><span style="background: black; color: #ffc66d">CultureInfo</span><span style="background: black; color: white">.CurrentCulture,
                             </span><span style="background: black; color: #cc7832">base</span><span style="background: black; color: white">.ErrorMessageString,
                             </span><span style="background: black; color: white">name, </span><span style="background: black; color: #cc7832">this</span><span style="background: black; color: white">.str1, </span><span style="background: black; color: #cc7832">this</span><span style="background: black; color: white">.str2);
    }
}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>All the validation logic takes place in the IsValid function. You can override the FormatErrorMessage function so that you can supply a custom error message for each instance that you want to validate if you so wish.</p>
<p>I&#8217;ll show you what I mean.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td style="background: black" valign="top" width="400">
        </p>
<pre class="code"><span style="background: black; color: #cc7832">public class </span><span style="background: black; color: #ffc66d">MyOrder </span><span style="background: black; color: white">: </span><span style="background: black; color: #ffc66d">CustomValidation
</span><span style="background: black; color: white">{
    [</span><span style="background: black; color: #ffc66d">TwoValue</span><span style="background: black; color: white">(</span><span style="background: black; color: #a5c25c">&quot;Yes&quot;</span><span style="background: black; color: white">,</span><span style="background: black; color: #a5c25c">&quot;No&quot;</span><span style="background: black; color: white">,
        ErrorMessage=</span><span style="background: black; color: #a5c25c">&quot;Only the values '{1}' and '{1}' are possible&quot;
                </span><span style="background: black; color: white">+ </span><span style="background: black; color: #a5c25c">&quot; for the {0} field&quot;</span><span style="background: black; color: white">)]
    </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">Value1 { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }

    [</span><span style="background: black; color: #ffc66d">TwoValue</span><span style="background: black; color: white">(</span><span style="background: black; color: #a5c25c">&quot;Yes&quot;</span><span style="background: black; color: white">, </span><span style="background: black; color: #a5c25c">&quot;No&quot;</span><span style="background: black; color: white">)]
    </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">Value2 { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>
  <br />Value2 in the above case will give you the default error message if the value is not &quot;Yes&quot; or &quot;No&quot;. However if Value1 is not &quot;Yes&quot; or &quot;No&quot; then the error message supplied will be used and the tokens replaced by the value entered, and the two values used for validation.</p>
<p>And that&#8217;s it. Pretty simple really.<br />
  <br />Cheers,</p>
<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2009/03/11/custom-validation-attributes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
