title: FluentValidation unique name validation using database
body: <p>I have <em>Category</em> model that has Name field, and every category name must be unique. I have made validation and it works when I try to create new Category but I have problem when trying to edit it. For now it's just checking if the name exists and of course it does when I try to edit same category.</p>
  
  <p>Model</p>
  
  <pre><code>[Validator(typeof(CategoryValidator))]
  public class Category
  {
      public int ID { get; set; }
      public string Name { get; set; }
      virtual public ICollection&lt;Image&gt; Images { get; set; }
  }
  
  public class CategoryValidator : AbstractValidator&lt;Category&gt;
  {
      public CategoryValidator()
      {
          RuleFor(x =&gt; x.Name).NotEmpty().WithMessage("Category name is required.").Must(UniqueName).WithMessage("This category name already exists.");
      }
  
      private bool UniqueName(string name)
      {
          ProjecteDataContext _db = new ProjecteDataContext();
          var category = _db.Categories.Where(x =&gt; x.Name.ToLower() == name.ToLower()).SingleOrDefault();
  
          if (category == null) return true;
          return false;
      }
  }
  </code></pre>
  
  <p>As you can see I have <code>UniqueName(string name)</code> function, but how can I pass ID, or whole model in it so I can check if it's same id as model I'm trying to edit then it pass. How could I pass something like <code>UniqueName(string name, int? id)</code>? I discovered FluentValidation only today and I can't figure out.</p>