title: Best Practices ViewModel Validation in ASP.NET MVC
body: <p>I am using <code>DataAnnotations</code> to validate my <code>ViewModel</code> on client side with <code>jquery.validate.unobtrusive</code> and on server side in <strong>ASP.NET MVC</strong> application.</p>
  
  <p>Not so long time ago, I figured out that I can write validation like this:</p>
  
  <pre><code>[Required(ErrorMessage = "{0} is required")]
  public string Name { get; set; }
  </code></pre>
  
  <p>That way I can easily define some general strings in <strong>config</strong> or in resources and always use it in <code>DataAnnotations</code>. So it will be easier to change validation messages in my whole application in future.</p>
  
  <p>Also I know that there is a <strong>FluentValidation</strong> library that allows to add validation rules to already existing <code>ViewModel</code>. I know that there is a problem with Add/Edit <code>ViewModels</code> that could have similar fields but different ValidationRules.</p>
  
  <p>Another problem that comes from client validation is that html newly added to <strong>DOM</strong> (using <strong>ajax request</strong>) should be parsed to enable validation. This is how I do it:</p>
  
  <pre><code>$('#some-ajax-form').data('validator', null); 
  $.validator.unobtrusive.parse('#some-ajax-form');
  </code></pre>
  
  <p>So I have some questions:</p>
  
  <ol>
  <li>Is there some other useful practises that could help centralize all validation rules in application?</li>
  <li>What's is a best way to solve Add/Edit <code>ViewModel</code> Validation problem? Can I use <code>DataAnnotations</code> with <strong>FluentValidation</strong> or separate Add and Edit <code>ViewModels</code> still is a best option?</li>
  <li>Is there any better way to initialize validation on new <strong>DOM</strong> elements that received with <strong>ajax call</strong> other that I mention?</li>
  </ol>
  
  <hr>
  
  <p>I'm not asking how to create my own <code>DataValidators</code> I know how to do it. I seeking of ways how to use them in more productive and easy maintainable way.</p>