Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FluentValidation unique name validation using database

I have Category 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.

Model

[Validator(typeof(CategoryValidator))] public class Category {     public int ID { get; set; }     public string Name { get; set; }     virtual public ICollection<Image> Images { get; set; } }  public class CategoryValidator : AbstractValidator<Category> {     public CategoryValidator()     {         RuleFor(x => 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 => x.Name.ToLower() == name.ToLower()).SingleOrDefault();          if (category == null) return true;         return false;     } } 

As you can see I have UniqueName(string name) 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 UniqueName(string name, int? id)? I discovered FluentValidation only today and I can't figure out.

like image 583
Stan Avatar asked Nov 15 '12 21:11

Stan


1 Answers

Predicate Validator (aka Must) has an overload, which accepts predicate with two parameters (validated object and property value). In your case predicate will have type Func<Category, string, bool>. So just add Category parameter to your unique name validation method:

private bool UniqueName(Category category, string name) {         ProjecteDataContext _db = new ProjecteDataContext();         var dbCategory = _db.Categories                             .Where(x => x.Name.ToLower() == name.ToLower())                             .SingleOrDefault();          if (dbCategory == null)              return true;          return dbCategory.ID == category.ID; } 
like image 130
Sergey Berezovskiy Avatar answered Sep 28 '22 18:09

Sergey Berezovskiy