Making use of multiple properties in a single validator

Oct 1, 2013 at 2:25 AM
Hi all,

I'm trying to validate a 'serial number' for a given device and ensure that it's unique before committing to the database, and so far the only issue I can see is that where i'm updating a record, the 'unique' validator needs to ignore the fact that the serial being saved already exists in the database because it's an update not an insert.
I can do this easily with code inside the controller itself or the repository, but would rather this happen inside the validation so that Model.IsValid returns false if someone tries to insert a duplicate serial number.

Looking at how RuleFor() works, I can see how to get a single property and pass it to a private method in order to do something, but how do I go about passing two properties to a method?
Coordinator
Oct 1, 2013 at 2:03 PM
You can use the Must validator for this. There are two overloads - the second accepts the instance being validated so you can use this to inspect other properties. Check out this page in the documentation for details:

https://fluentvalidation.codeplex.com/wikipage?title=Validators&referringTitle=Documentation&ANCHOR#Predicate