Validate list of ints in FluentValidation -
how validate list of ints using fluent validation?
my model has:
public list<int> windowglassitems { get; set; }
model validator has
rulefor(x => x.windowglassitems).setcollectionvalidator(new windowglassitemsvalidator()); public class windowglassitemsvalidator : abstractvalidator<int> { public windowglassitemsvalidator() { rulefor(x=>x).notnull().notequal(0).withmessage("glass req"); } }
im getting:
property name not automatically determined expression x => x. please specify either custom property name calling 'withname'.
you seeing error because rulefor method expecting property specified. have been unable collectionvalidators work primitive types have. instead, use custom validation method must
this.
the problem have approach unable avoid repeating error message across 2 validations. if don't require when list null, can leave out after notnull
call.
rulefor(x => x.windowglassitems) //stop on first failure avoid exception in method null value .cascade(cascademode.stoponfirstfailure) .notnull().withmessage("glass req") .must(notequalzero).withmessage("glass req"); private bool notequalzero(list<int> ints) { return ints.all(i => != 0); }
Comments
Post a Comment