asp.net mvc - FluentValidation on number issue -


i using fluentvalidation in asp.net mvc 3 application.

i have maxnumberteammembers property in view model such:

/// <summary> /// gets or sets maximum number of team members. /// </summary> public int maxnumberteammembers { get; set; } 

i want know if following ruleset possible:

  • on front end view, if textbox empty want "maxnumberteammembers required" message displayed
  • if number entered less 1 want message display "maxnumberteammembers should greater or equal 1".

what ruleset above like?

i have following not work on greaterthan part if enter 0:

rulefor(x => x.maxnumberteammembers)      .notempty()      .withmessage("max. number of team members required")      .greaterthan(0)      .withmessage("max. number of team members must greater 0"); 

update 2011-02-14:

rulefor(x => x.minnumbercharacterscitation)    .notnull()    .withmessage("min. number of characters citation required")    .greaterthanorequalto(1)    .withmessage("min. number of characters citation must greater or equal 1")    .lessthanorequalto(x => x.maxnumbercharacterscitation)    .withmessage("min. number of characters must less or equal max. number of characters"); 

if want handle empty case need nullable integer on model because otherwise default model binder automatically add validation error when tries parse empty string non-nullable integer:

public int? maxnumberteammembers { get; set; } 

and have following validation rules on property:

rulefor(x => x.maxnumberteammembers)     .notempty()     .withmessage("max. number of team members required")     .must(x => x.value > 0)     .when(x => x.maxnumberteammembers != null)     .withmessage("max. number of team members must greater 0"); 


update:

the following works fine latest version of fluentvalidation:

rulefor(x => x.maxnumberteammembers)     .notnull()     .withmessage("max. number of team members required")     .greaterthan(0)     .withmessage("max. number of team members must greater 0"); 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -