Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

New Post: Validating using 2 fields

$
0
0

I got a requirement that a new product name should be unique.

So I created this rule (very new to FV btw)

RuleFor(product => product.Name)                .NotEmpty()                .Must(_productsService.ProductNameIsUnique);

Which use this method in my repository:

public bool ProductNameIsUnique(string name){    Product product = _productsRepository.GetByName(name);    return product == null;}

 

All is well, but the thing is when I UPDATE an existing product, this will fail (unless you changed the product name)

So I need to do something in the line of "where productname == name && productid !=id", if that make any sense?


Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>