c# - FluentValidation Validate Method throwing an excepion -
i'm having issues using fluentvalidation library.
i have small model
`
[fluentvalidation.attributes.validator(typeof(personvalidator))] public class personmodel { public string name { get; set; } public nullable<short> type { get; set; } }
`
have validator class
public class personvalidator : abstractvalidator<personmodel> { public personvalidator() { rulefor(x => x.name) .length(1, 5) .withlocalizedmessage(() => basevalidationresource.lengthvalidationmessage, 1, 5); } }
and have controller
public actionresult index() { var model = new personmodel(); model.name = "john doe"; var validator = new personvalidator(); var results = validator.validate(model); var error = getmodelerrors(); return view(model); }
so far good, issue when progam executing , gets line ; var results = validator.validate(model);
throws systemformatexception.
instead of throwing exception, shouldn't validate method return object containing boolean field indicates if model valid , list of errors.
ps : know particular validation can done using dataannotations want use fluentvalidation because more flexible.
thanks in advance help.
as @ric pointed out string formatting issue on resource file.
on validor had line
rulefor(x => x.name) .length(1, 5) .withlocalizedmessage(() => basevalidationresource.lengthvalidationmessage, 1, 5);
which pointed resource file , passed 2 parameters. on resource file expecting 3 parameters (property name, min value, max value).
however using wrong annotation. below before , after version of the resource file.
Comments
Post a Comment