c# - Fluent Validation Inconsistent with ASP.NET MVC 5 -


i'm using fluent validation v5.5 asp.net v5.2.2 , i'm getting inconsistent results validation.

my view model is:

public class quoteviewmodel {     [display(name = @"day")]     public int dateofbirthday { get; set; }      [display(name = @"month")]     public int dateofbirthmonth { get; set; }      [display(name = @"year")]     public int dateofbirthyear { get; set; }      [display(name = @"gender")]     public gender? gender { get; set; }      [display(name = @"state")]     public int stateid { get; set; } } 

my controller method is:

public actionresult quote(quoteviewmodel viewmodel) {     var _validator = new quotevalidator();     var results = _validator.validate(viewmodel);      if (!modelstate.isvalid)     {         return json(false);     }      return json(true); } 

my validator is:

public class quotevalidator : abstractvalidator<quoteviewmodel> {     public quotevalidator()     {         rulefor(x => x.gender).notempty();         rulefor(x => x.stateid).notempty();         rulefor(x => x.dateofbirthday).notempty().inclusivebetween(1, 31);         rulefor(x => x.dateofbirthmonth).notempty().inclusivebetween(1, 12);         rulefor(x => x.dateofbirthyear).notempty().lessthanorequalto(datetime.utcnow.year);     } } 

i'm running test posts blank value form fields. view model fields retain default values after view model object created.

for comparison, in controller i'm running validation explicitly , results aren't consistent validation result in modelstate.

modelstate showing 4 errors, triggered notempty rules. notempty on nullable enum gender doesn't seem trigger.

the explicit validation returning 7 out of 8 errors, lessthanorequalto rule won't fire since dateofbirthyear defaults zero.

my pain point can't figure out why modelstate missing notempty error on nullable enum gender.

the way i've been able trigger error post gender value.

please help.

edit:

after stepping through code, appears issue related fluent validation requiredfluentvalidationpropertyvalidator. gender field nullable value type bound null. following snippet requiredfluentvalidationpropertyvalidator prevents validation:

shouldvalidate = isnonnullablevaluetype && nullwasspecified; 

!modelstate.isvalid doesn't use validation result uses defaulf mvc validation (that can added through dataannotations). have check !results.isvalid instead contains validation result of quotevalidator.

if want use default modelstate.isvalid have mark model validator attribute:

[validator(typeof(quotevalidator))] public class quoteviewmodel {     [display(name = @"day")]     public int dateofbirthday { get; set; }      [display(name = @"month")]     public int dateofbirthmonth { get; set; }      [display(name = @"year")]     public int dateofbirthyear { get; set; }      [display(name = @"gender")]     public gender? gender { get; set; }      [display(name = @"state")]     public int stateid { get; set; } } 

and add following line application_start method:

protected void application_start() {     fluentvalidationmodelvalidatorprovider.configure(); } 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -